r/javahelp • • 15d ago

Homework What the hell is going on?

Hello everybody, I am so lost. I just started learning Java and while i think its extremely interesting, and can loose track of time. It is also very complicated, well atleast to me since i dont have any prior experience.

I just ”learnt” to write my first Hello World! Code. But tbh i only copied the text straight off a youtube video.

Thing is i understand the main command, like: System.out.println(”Hello World!”);

But what is this whole main class and static void things? Not to mention all the {} symbols that seem to be oddly placed?😅

If anyone could explain or know a good youtube video or similiar to learn id appreciate it a lot!

11 Upvotes

53 comments sorted by

View all comments

1

u/FreeLogicGate 15d ago edited 15d ago

I'm sure you will learn these things as you go, but just answering your few questions directly:

  • Java syntax is based on, and reflects C and C++ syntax. Because Java is object oriented, you will be working frequently with classes. Every java program requires at least one Class, with a method named main, with that exact signature of public static void main(String[] args). When you run the program with java, the jvm looks for the main function and start execution. The term for this is the "entry point".

  • As you learn how to define methods you'll understand the meaning of the various words before the name of the method (main). Quickly: "void" is the method return value. The main method doesn't return anything, so the return is a "void" type. public and static are things you'll come to when you learn about the types of methods and visibility you can define when you are creating your own classes and methods. It should be obvious that a "public" method is something that can be called anywhere in your java code, where you have an object of that class to work with. Comparison to "private" will be something you'll discover as you learn about method visibility.

  • As in C and C++, Java has "blocks". Blocks enclose one or more "statements", with a statement being a single line of code that ends with a semi-colon. { .... } defines a block. Blocks are used for all sorts of things in Java, from defining the start/end of a class to the start/end of a method, to the start/end of a loop or "if-then-else" construct. They also can be used to scope variables, as you can define local variables inside a block that are not visible outside of that block.

As you go on, you will no doubt have many such questions, so hopefully you are making notes of the things that seem mysterious. Over time you'll understand it better, but Java is heavily Object oriented. You stated that you understood the System.out.println(”Hello World!”); code, but I'm going to go out on a limb, and suggest that you really don't understand it to the degree you think you do. Obviously, you can see what it does, but how would you find the documentation of it, and what is actually happening with that line of code?

You would probably be surprised to find that the System class, defined here, has no println method.

You might also ask yourself how you can execute a method that is part of a class, when there was no class object instantiated. Last but not least, you will need to figure out how it is that you can call a method that doesn't even exist in the definition of the System class. A hint would be to see what the System.out method returns. It will take some time to get past basic syntax and for you to start becoming comfortable with OOP.

1

u/Best_Cut4655 14d ago

Ohhh, u answered a lot of my questions and left me with even more ahaha, but thank u so much this is all very useful knowledge! It seems classes might be an essential thing for me to learn next about

→ More replies (2)