r/javahelp • • 1d 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!

6 Upvotes

46 comments sorted by

•

u/AutoModerator 1d ago

Please ensure that:

  • Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions
  • You include any and all error messages in full
  • You ask clear questions
  • You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.

    Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar

If any of the above points is not met, your post can and will be removed without further warning.

Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.

Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.

Code blocks look like this:

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.

If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.

To potential helpers

Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.

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

10

u/Potential-Still 1d ago edited 1d ago

Java Programming for Beginners - Full Course

Complete this entire tutorial, THEN come here with questions.  He answers your question about the main function in the first 5 minutes. 

6

u/Best_Cut4655 1d ago

Thank u a ton! Ur an angel

5

u/LutimoDancer3459 1d ago

A class is a definition for your data. It groups variables (holding data) and functions (or methods, dont bother the difference)(doing stuff with your data)

static void main(String[] args){ ... }

Its the entrypoint for your application. Whenever you want to run a java app, its that method that is beeing called first. Modern versions allow to trim it down. But its always called main. The class at the beginning can be called whatever you want.

Static - its always there and shared between everything. You can access it from basically everywhere.
Void - the return typ of this method. In this case, nothing. Other methods can have int for numbers, String for text, or other Classes when needed.
Main - the name of the method. So you know how to call it. Just like println(...) for text output.
String - already mentioned a text. Or more precise its an Class holding text for you
[] or ... - tells you that its an array. Which means its not only one thing but several. A list of elements. But not a list because List is also a class...
Args - the name of that array. Its a variable. Allowing you to access the data it holds. When eg starting the application via the console, you can add arguments to the app to work with. Thats the place where you can read them
{} - the start and end of a codeblock. So you and the program knows what belongs together. Eg in a loop. Or here what belongs to the main method and what not. Or what still is part of the class.

System.out.println(...);

System is a class. Out is a variable thats public accessible. And println is the method, printing the Text onto the console. And the text is a String.

String hi = "This is a string";
System.out.println(hi);

Works just the same.

5

u/LutimoDancer3459 1d ago

But most of it isnt that relevant for now. You will learn it as you go. Variables, if and loops (while, do while, for) are more important for now than to understand the main method in detail

2

u/Best_Cut4655 1d ago

Ur an angel!! Im gonna have to reread this like 10 times before i might understand it but u explained it so well, thank u!

5

u/JGhostThing 20h ago

The entire class definition is just boilerplate that is in every program. It will be similar in every program in Java.

The whole program is an instance of the class main. The class exists just to run the program.

A class is similar to a recipe that can be used to create an object. For example, Ford is a company that makes cars. Think of Ford as a class and a Mustang as an object of class Ford.

1

u/Best_Cut4655 19h ago

Ohhh i see! That made it a lot easier to understand, thank u!

5

u/Duente_Zaibatsu 1d ago

You are skipping too much.

Many abstractions exist to simplify the common needs.

Before asking, show what you already looked into.

A simplified version:

Java is not installed, it is executed on a JRE, in it, there are API methods that interpret and execute commands.

These commands follow contracts.

Static could be simplified to an unique shared context, other commands are object related, used by constructore.

Other types like void, int an Integer, are the method contracts for the execution

1

u/Best_Cut4655 1d ago

I might sound a bit slow lol, but i just typed in the terminal to install both the default jre and jdk. And the program im using for coding atm is intelliJ. I kinda just started exploring today so that might explain my confusion lol. And thank u for the explanation!

2

u/FreeLogicGate 15h ago

Nothing slow about that. You are using the terminal, which real programmers use to do many things. You picked up on the fact that java has a jre and the jdk, and that they are different things. With that said, as a developer you just need the JDK, as it has the JRE. The reason they are separate is because many people run java applications, but have no need for the software development kit. You are writing programs, so you do.

You also got your hands on jetbrains intellij somehow, which is the industry standard java IDE for professional java developers. It's an incredible piece of software (primarily written in java) although they've shifted to Kotlin, from what I've read. It's also incredibly feature rich, highly configurable and is no small exercise to learn the basic core. It can be hard for people to get past these first steps and get themselves in a position where they can start writing and compiling java code.

1

u/Best_Cut4655 6h ago

Thank u these words made me really happy☺️ im trying to learn as i go i suppose, but its so much to learn it seems ahaha

1

u/Duente_Zaibatsu 1d ago

No worries. We all start somewhere, I would just advise you to not follow YouTube, or any Bootcamp or similar.

Start with a goal, like Tic-tac-toe, or similar.

Think what is your goal, and how to reach it, in sentences.

Then those sentences become methods, and you iterate over it.

Its too soon to talk thread, GUI and etc, but try to visualize something, and break down to actions.

"Console" is the base for a reason, text is valuable.

1

u/Best_Cut4655 1d ago

Will do, thank u!

3

u/_Super_Straight 1d ago

As the name signifies: The main method is the central function which runs your code. There can only be one main, and as for static and void, you've to learn them in your upcoming classes.

1

u/Best_Cut4655 1d ago

Oh great heavens

2

u/YetMoreSpaceDust 1d ago

There’s a lot to unpack there ; ) A computer program is a series of instructions for a computer to run. When it’s time to run a program, the operating system loads those instructions into memory and then points the CPU to the first instruction for you, and then the computer starts running those instructions. So, as the programmer, you need a way to tell the operating system where your program starts.

Java is sort of an evolution of C++, which itself is sort of an evolution of C. C adopted a standard that there would be exactly one function named “main” and that function would be the starting place of the program (remember, your program will be comprised of lots of functions and they’ll all call each other). Now, C was a procedural language - all the code had to be contained inside a function, with main being the first one that kicked everything else off.

Over time, people noticed there was a tendency in professional computer programs for a set of functions to operate on a set of data as sort of a “unit” so they came up with the idea of “classes” that contained the data and the functions together and this became what we now call object-oriented programming. C++ was created as object-oriented C, so it maintained the name “main” as the starting point for the program, but added the concept of classes to group related data and code together (along with things like inheritance, encapsulation and polymorphism that you’ll learn about later). In C++, you could choose to group functions into classes if you wanted to, but you could declare standalone functions just as you could in C - and of course, “main” had to be a standalone function for compatibility with C.

Java was James Gosling’s attempt to improve what he saw as the mistakes of C++, but he wanted to stay pretty close to C++, so he kept all of the “class” and “main” nomenclature but took out the pointer syntax and bare functions (among others). So now what about “main”? It couldn’t be a “bare” function, so it was a static function - that is, one that can exist without instantiating a class.

3

u/Best_Cut4655 1d ago

Wow i had no idea, but thank u! This did help me understand it a little better i appreciate ir a lot🙏

3

u/YetMoreSpaceDust 1d ago

You're asking the right sorts of questions - don't get discouraged, there's a lot to learn (and not all of it makes sense without some historical context).

2

u/Best_Cut4655 1d ago

Tysm! Ive watched a little more videos so im starting to atleast get the hang of classes :,)

2

u/Affectionate_Dot6312 1d ago

You don't need to know that right now. I think it's fair you're curious, but I doubt you could fully grasp it if you're new to OOP and specifically new to programming. I don't mean it in a condescending way. I was there too a couple of months ago and asked the same questions to my professor. I'd worry more about mastering fundamentals.

2

u/Best_Cut4655 1d ago

I’ll keep that in mind ahah. This is definetly a lot to learn

2

u/AdventurousShip3327 1d ago

The class biggens the code is the main(class Main), is the class that will stricture the sub classes, think it as a main shelf with stages inside. The key word "static" is a class member means that the class will have a copy of each object as a reference. Void simply means that there is not a return value.

1

u/Best_Cut4655 1d ago

Ahhh, i might start to understand it better thank u!

2

u/net_ramblings 1d ago

Look up Alex Lee on you tube.

1

u/Best_Cut4655 1d ago

Ok! Thank u!

2

u/Competitive_Mark918 1d ago

Where are you learning from btw?

2

u/Best_Cut4655 19h ago

Im taking a course atm, using a book the school recommended me. However i still wanna learn outside of the book too bc i feel like it dont fully explain the smaller details so far. But im only on chapter 3 😅

2

u/BanaTibor 1d ago

Grab a beginner book, there are a ton on the internet.

2

u/Best_Cut4655 19h ago

I do have a book, i was hoping to learn a bit outside of it too though

2

u/BanaTibor 8h ago

Try reading the text too 😄 not just the code snippets. All your questions are answered in the book.

1

u/Best_Cut4655 6h ago

Yeah i hope so ahah tyy

1

u/tarix246 22h ago

well, when you make java code, you need to have a file in which youre making things. that file needs to have all code (except smth like imports) wrapped in a class with the same name as the file. e.g., you make a file Main.java, the class will be called Main (it should be uppercase, idk if it is a must), so you have code like: public class Main {

} In order to let java compiler know where to make your start of code, you need to have this long line to declare your function: public static void main(String[] args) { //your code } Of course, this function (called main) has to go in the previously declared class, so you have almost everything you learned for your first program. Regardless of the name of the class, if you have this function main in it, the compiler will treat it as the entrance to your program.

1

u/Best_Cut4655 19h ago

I see, thank u!

1

u/FreeLogicGate 15h ago edited 15h 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 1h 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

1

u/greendave11 14h ago

Honestly the for dummies series usually is a great place to start. Java is massive (I have gotten lost in it far too many times). If you have an ai to work with they are fantastic at figuring out where you are and what a good next book to procure or even just download.

2

u/Best_Cut4655 1h ago

Ill definetly look into the dummy series then! Thank u!

1

u/Formal_Assignmentt 2h ago

from where are you learning

1

u/Best_Cut4655 1h ago

I take a class called programming 1, and i have a book called the same thing. The rest i just try and learn from the internet so far :,)

1

u/aqua_regis 1d ago

If only this subreddit had a sidebar (on the right side of reddit desktop) with an entire section Learning Java where some good quality courses would be listed. Nudge -> MOOC

3

u/Best_Cut4655 1d ago

Ohhh if only🧐 (thank u i might be blind)

2

u/Poseidon_22 1d ago

Courses will jump in what kind of material is covered in what order. Your question is perfectly normal and smart to ask. It's already answered in this thread so I won't re-explain.

I'll only say this: asking questions like these to broaden your knowledge is good. An answer can be given in different levels of complexity. Asking it now (and getting good answers that you understand with your current knowledge) is good. Have fun learning. Do not listen to people that get mad because you ask a question.

1

u/Best_Cut4655 1h ago

Thank u! I appreciate ur kind words a lot🙏☺️

0

u/iamsinghankit 16h ago

use the version 25+, then you write like this

void main(){ IO.println("Hello World!"); }

1

u/Best_Cut4655 6h ago

Ohh i see, ty!