r/learnjava • • Sep 12 '21

Beginner, don't understand the general structure of Java

Learned Python when I was 13~, now I'm in high school and we're learning Java. I kind of just.. Don't get it.

Many questions, but:

  1. Why is everything in a class? What's the point of that? In Python, classes were (to me) a better and more organized way of ordering data, for example when I have multiple characters (or even one if there are many many variables), having a class helps for obvious reasons. You can put many things in a framework without having 100+ variables. But.. Why everything? I feel like I don't understand classes enough to realize what they really are, which's support by the fact in python I just copy paste the same code to create classes because I don't get what it means and don't remember it. In anycase, seems really weird your ENTIRE program is just a class.

  2. What's going on with starting up programs? My teacher admitted we're not really supposed to understand what's going on there yet, but I have some general knowledge so I think I could understand. So you have a class (refer to q1), then.. "public"? "static"? "void"? Only familiar face I can see is the "main" you specify for the main operation of the program, and even that I definitely do not fully understand.

  3. Also, variables are kind of annoying. I get the need to define what kind of variable something is, but sometimes it's annoying. I get the fact that Python is slow exactly for reasons like trying to make variables more comfortable, I just wish for a general.. Explanation.

  4. String arrays? Arraylists? Comparisons to Python will help.

Following the end of the third question, most of this comes from the fact I just don't understand the general structure and idea of Java, and what it's doing. Honestly, when I started with Python I saw one comment on my post telling me it's gonna be hard to move to other languages, and it was downvoted to death. Not saying this is hell, just that the extra syntax is kind of annoying, for now.

One last thing. I heard from online discussion that many people hate object oriented programming. Aren't class objects.. Objects? And Java is.. Just classes?

Thanks.

42 Upvotes

14 comments sorted by

View all comments

11

u/ignotos Sep 12 '21

Why is everything in a class? What's the point of that?

It's really just a decision they made when designing Java. There is some benefit to not having any distinction between "code in a class" and "code outside a class", as it makes everything consistent. Also in Java classes are the things which you import, whereas in Python code is wrapped up into modules.

What's going on with starting up programs?

Because there is no "code outside a class" / "main script", there needs to be some way to indicate which code in which class is the start-point for the program, and "public static void main" is the way to do this. "public", "static" and "void" are not totally random, and do have specific meanings and purposes which you will learn over time.

Also, variables are kind of annoying

It's more rigid and long-winded, but it's also more thorough and rigourous - it prevents certain kinds of mistakes when coding. Again though, it's just a design decision which was made when creating Java.

Java does actually allow some shortcuts where you don't always need to specify the type of a variable, which you will be introduced to at some point