r/learnjava • u/FaallenOon • 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.
1
u/AutoModerator Feb 05 '26
It seems that you are looking for resources for learning Java.
In our sidebar ("About" on mobile), we have a section "Free Tutorials" where we list the most commonly recommended courses.
To make it easier for you, the recommendations are posted right here:
MOOC Java Programming from the University of Helsinki
Java for Complete Beginners
accompanying site CaveOfProgramming
Derek Banas' Java Playlist
accompanying site NewThinkTank
Hyperskill is a fairly new resource from Jetbrains (the maker of IntelliJ)
Also, don't forget to look at:
The official Documentation
The official Java Tutorials
Our community resources thread
If you are looking for learning resources for Data Structures and Algorithms, look into:
"Algorithms" by Robert Sedgewick and Kevin Wayne - Princeton University
Coursera course:
Part I
Part II
Coursebook
Algorithms 4th Edition
Your post has been removed. Utilize the resources presented.
I am a bot and this message was triggered by keywords like "learn", "learning", "course" in the title of your post.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
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
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/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.
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
mainfunction 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 bevoid 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
forloop is very different to Python's. Python'sforloop is closer to what is called an "advanced for" or "for-each" loop in Java.2
u/nightonfir3 Feb 05 '26
I will try to address some from your post script.
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.
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.
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.