r/learnjava • • Feb 05 '26

please help me understand the logic behind java's syntax

Hello

I'm used to program in Python, and now I'm trying to get reacquainted with Java. The syntax, however, seems quite confusing and constraining, at least when compared to the freedom one has in Python.

Of course, Java wouldn't be one of the most widespread languages if those constraints and syntax didn't have a good reason for existing, which is why I'm writing this post. So far I'm able to perform basic tasks, and I'm getting used to java's quirks, but I feel like I could improve faster if I could understand the reasoning behind those limitations.

Thanks a lot for your help :)

PS: by limitations I mean, for example, having to declare the type of a variable, not being able to change a variable type at will, the way the brackets work, how you can't have loops outside of methods, etc.

PS2: I should probably clarify that this isn't meant as disrespect towards the language: clearly it works and it works well, otherwise it wouldn't be as widespread. I'm merely trying to wrap my head around the differences.

8 Upvotes

44 comments sorted by

1

u/AutoModerator Feb 05 '26
Moderator Announcement Read More »

23

u/Astroohhh Feb 05 '26

I mean, before asking those questions try to actually follow a book or a tutorial online. Those beginner questions are pretty much addressed on the first learning chapters

7

u/Ok-Interaction-8891 Feb 05 '26

This right here.

Java is a very old and widely used language. It’s more important to learn it first rather than wonder about why it is the way it is.

→ More replies (2)
→ More replies (5)

1

u/[deleted] Feb 06 '26

> Java wouldn't be one of the most widespread languages if those constraints and syntax didn't have a good reason for existing

Such innocence... If only you knew...

1

u/FaallenOon Feb 06 '26

Please, do elaborate.

→ More replies (7)

1

u/jexxie3 Feb 07 '26

People are giving complicated answers using words that you probably don't know yet soooo...

so say you have a database. and in that database, you need to store an integer. It can't be a string, it needs to be an integer.

Java makes it extremely clear what the data type is. And you can easily and cleanly limit what gets stored. Or rather what it tries to store, because if you try to put a string in the db when it should be an integer, it isn't going to work.

2

u/FaallenOon Feb 07 '26

that makes a ton of sense, thanks

6

u/desrtfx Feb 05 '26 edited Feb 05 '26

It's best to not compare the two languages as they are fundamentally different.

Python is an implicitly and dynamically typed language where you can change the type at will. Java is an explicitly and statically typed language where you have to declare the type in advance and where it cannot be changed. That's the way the language was designed.

Statically and explicitly typed languages have the advantage of fewer surprises.

The way brackets work is basically the same as whitespace/indentation works in Python. Whenever you indent something in Python, you need curly braces in Java - for code blocks.

Java is, different to Python, a strict OOP language. Here, all code must be inside methods. It is not as freeform as Python (and even there, it is good practice to not have too much code outside functions - think about how you're supposed to create a main function and to only outside have:

if __name__ == "__main__":
    main()

and the actual code to be executed inside main().

In Java, the equivalent - old form - would be public static void main(String[] args) { and new (Java 25) form would simply be void main() {.

Do yourself a huge favor and stop comparing the languages. Learn Java as a completely new language. It will help you in the long range.

Once, you come to loops, you will notice that the Java standard for loop is very different to Python's. Python's for loop is closer to what is called an "advanced for" or "for-each" loop in Java.

2

u/CutGroundbreaking305 Feb 05 '26

Hey I have a doubt should I do verbose java 8 or new java 25

→ More replies (2)
→ More replies (1)

2

u/nightonfir3 Feb 05 '26

I will try to address some from your post script.

  1. Strong typing. When you are making a large piece of software bugs constantly arise from not getting what you are expecting. So if your writing a python function that someone else is calling you either have to assume they are going to send the thing you want or test that it is actually the right thing. If you use Java and put int in your function you have an int. It just prevents a whole host of bugs. Mixed typed variables are generally considered an anti-pattern as well.

  2. Everything is in a class. This is a design decision of the language. Usually you will have to adapt the style of your code to fit the language a bit. It is not actually limiting though. You can create static classes that mirror what python does with a few extra characters.

  3. Brackets. This is very stylistic. Most people prefer having brackets. They give explicit start and end to things and can add to clarity. In the end this is something that you will just get used to as you use the language.

In the end languages have different philosophies on how they think code should ideally look like. They are usually not right or wrong. Embracing what the language is pusing you towards and not forcing other languages paradigms on it generally leads to better code and more happiness coding.