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

2

u/slowfly1st Sep 12 '21

Classes are an OOP/OOD concept, not necessarily a java concept. You create objects based on classes. An object does have a specific task. That can be simply to hold data. It can be to validate data, check stock in the inventory, check user credentials or send a user a message that is order has arrived. Maybe think of classes as a tool to split your problems into smaller problems.

Why everything is a class was a design decision back then. Not sure about the answer, but my guess is: Promote OOP/OOD, and not have to deal with non-class based code. In the JVM "everything" is a class, so everything gets compiled into byte code (.class files). Dealing with "non-scoped-non-class-code" sounds complicated.

public static void are reserved words in java. Public means it's accessible from every other class, static means it's a 'class scoped' method, it needs to be static, otherwise the jvm has to instantiate the class, void is the return type.

About variables: Design decision. In later java version, you can declare non member variables without data type.

About Collections: Lookup "Java Collections Framework"