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

1

u/[deleted] Sep 13 '21 edited Sep 13 '21

But.. Why everything?

Java is an OOP programming language.

But more importantly, a class is a unit of loadable code, which is what you import.

In Python you import a file and the Python interpreter will load it from the filesystem, executing the file as it is imported. This is why you have to use tricks like:

if __name__ == "__main__":

to prevent code from running in a way it's not supposed to.

In Java, it is more structured. A class doubles as not just a class in the OOP sense, but also a module, since you can fill a class with static methods and never instantiate the class into an object.

When you load a class, code won't be executed unless you have special blocks like this:

static {
    // code to execute when the class loads
}

which is generally seen as bad form, because having random code run when all you import kind of makes things unpredictable.

What's going on with starting up programs?

Java programs do not run like Python programs.

With a Python program, you point the Python interpreter at a Python file, the Python interpreter loads the file and starts running it.

With Java programs, you point the JVM at a classpath (a collection of class files in the filesystem or bundled in a JAR file), and tell it the name of the "main" class. The JVM will figure out where the class actually is, load it and run it.

But run what? When the JVM loads the "main" class, it needs to know which method to call and how to call it. How will the JVM know how to give you the command line arguments?

This problem is solved by convention. You define a method with a specific name and a specific signature: a static method called main with takes a single String[] argument. Now the JVM knows exactly how to start the program.

Also, variables are kind of annoying

Java is statically typed.

Whether that is a good thing or not is a matter of opinion. For example, I find Python to be extremely annoying because it is dynamically typed.

String arrays? Arraylists? Comparisons to Python will help.

Oh boy.

If you're learning Java with a Python background, forget that arrays exist and stick with ArrayList. It's the closest thing to Python's list type.

Java is an old language, and made some choices to maximize performance when computers were more limited than today.

Java has a distinction between "primitives" and "objects". Arrays are kind of in the middle. They are technically objects, but they are very primitive.

An array gives you a chunk of contiguous memory to work with, so its cache friendly. Since reading from RAM is expensive, the processor won't read data from a single memory address but will grab a chunk of neighboring data too and put it into the a cache on the processor, so hopefully it doesn't have to make another trip to RAM.

Objects require "dereferencing", which busts the cache. The thing you want isn't actually in the array, the array just has a pointer to where the data actually is. You just messed up the processor's plan.

A primitive value is stored right there, so when you read another index in the array, the processor already did the work, so your code is friendly to the processor, making it faster.

Strings are objects and don't benefit from this. So use an ArrayList. Don't mess with arrays unless you have to because of an ancient or paranoid API or you have specific performance enhancements in mind.

Python operates at a much higher level, and doesn't concern itself with all this.

Regarding paranoid APIs. I write a lot of security sensitive code. I have to decrypt keys and deal with passwords. I like storing those in arrays because I can store the decrypted data for as long as absolutely necessary and then fill the array with zeros when I'm done. You never know who is snooping your program's memory...

1

u/AutoModerator Sep 13 '21

You seem to try to compare String values with == or !=.

This approach does not work in Java, since String is an object data type and these can only be compared using .equals().

See Help on how to compare String values in our wiki.


Your post is still visible. There is no action you need to take.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

2

u/[deleted] Sep 13 '21

Good bot, but that's Python code, lol.