First Program in Java
A "Hello, World!" program is a basic code that displays the message "Hello, World!" on the screen. It's commonly used to introduce beginners to a new programming language.
// This is a comment. It's like a note for humans to read, but the computer ignores it.
// This is the start of our program. It's a basic rule in Java to have a class definition.
class HelloWorld {
// This is the main method. It's where the program starts running.
public static void main(String[] args) {
// This line prints the words "Hello, World!" on the screen.
System.out.println("Hello, World!");
}
}
Now, let's understand it step by step:
- Comments: Lines that start with
//
are comments. They're just for people to understand the code, and the computer skips over them. - Class Definition: The program begins with the definition of a class named
HelloWorld
. Think of a class as a container for your code. - Main Method: Inside the class, there's a method (a set of instructions) called
main
. In Java, every program needs amain
method because it's where the computer starts executing the code. - Print Statement: Inside the
main
method, there's a line that saysSystem.out.println("Hello, World!");
. This line makes the computer print the words "Hello, World!" on the screen. TheSystem.out.println
part is like telling the computer to display something.
So, in simple terms, this program is like a recipe: it says, "Hey computer, when you start running, go to the main
method inside the HelloWorld
class, and then print 'Hello, World!' on the screen."
Don't worry if some terms like "class" or "method" seem a bit confusing now. As you learn more, these concepts will become clearer. Just think of this program as a friendly introduction to the world of Java programming!