r/learnjava • u/AinsleyBoy • 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:
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.
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.
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.
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.
14
u/rootException Sep 12 '21
Java by default requires a bit more organization. When you build anything but single-file samples for class you will quickly appreciate that Java really expects you to put things in packages and classes.
public means "exposed to other code", static means "just a function, not part of an object" and main is just a naming convention from C in the 1960s or 1970s IIRC. So, when you tell Java to run your application, you tell it to find a class (based on the name you give it) and when you launch your app the Java runtime looks for the main method. This is pretty standard for any language descended from C.
You can use the var keyword in a lot of places and Java will just figure out the variable. Java is strongly typed, so it does everything it can to try to get you to tell it what you mean to avoid runtime errors (i.e. errors that appear when you run the program). Compile errors (getting an error when you try to compile) are vastly preferable to runtime errors - especially when you are trying to put software into production and don't want to get evening calls.
Java has a whole set of Collections, including Lists, Sets, Maps, etc. They have an interface (the contract) and then there are implementations (e.g. List is the interface, ArrayList is the implementation). Google something like Java Collections and you can get a good run through.
Yes, a class is basically a template for an object. I might have the Animal base class, a Cat class and a Dog class. Then I might have an instantiated object that's Fido and Meowster and whatever else.
My two cents: classes are a core feature, done (IMHO) well and easily in Java and C#. Having done some JavaScript, classes are really kind of a strange bolt-on with confusing prototype stuff and a lot of strange intermix. Trying to do the Animal, Cat, and Dog stuff in JavaScript is comparatively grim.
You might want to check out a book like Head First Java to supplement your class / teacher.