What is “Hello, World!”?
In the world of programming, the “Hello, World!” program is the very first step for many who are just starting their coding journey. It’s a simple program that displays the message “Hello, World!” on the screen.
Now, let’s write our Java “Hello, World!” program and walk through it step by step:
1 2 3 4 5 |
public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, World!"); } } |
public class HelloWorld
This line marks the beginning of the program.
public
means that this class can be accessed by any other class from anywhere in the program.class
is a blueprint for creating objects in Java, but for now, think of it as a container for our program.HelloWorld
is the name of the class, and it must match the filename (in this case,HelloWorld.java
). Java is very picky about names, and it expects the file name to match the public class name.
public static void main(String[ ] args)
This line is the starting line in a race – it’s where everything begins.
public
: This keyword indicates that the method is accessible from any other class or package.static
: This keyword signifies that the method belongs to the class itself rather than to an instance of the class. This method can be called without creating an object of the class, making it accessible directly through the class.void
: This keyword specifies that the method does not return any value.main
: It serves as the starting point of the program. When the program is executed, the Java Virtual Machine (JVM) looks for themain
method to begin execution.(String[ ] args)
: Theargs
parameter is an array that allows the program to receive command-line arguments when it is run. Although not utilized in this specific case, it is available to capture and process any input passed to the program from the command line.
System.out.println(“Hello, World!”);
This line tells the computer to print out the message on the screen.
System
: This is a class in Java that contains useful tools for input and output.out
: This is an instance (or object) of thePrintStream
class.println
: This method tells Java to print whatever comes inside the parentheses and move the cursor to the next line afterward."Hello, World!"
: This is the actual text we want to display. It’s called a string literal, and it’s always wrapped in double quotes.
Putting It All Together
When you run this program, here’s what happens:
- Java finds the
HelloWorld
class and looks for themain
method. - The program starts executing inside
main
. System.out.println("Hello, World!");
is executed, and “Hello, World!” is displayed on the screen.- The program ends because there’s no more code to run.
Don’t worry if some of these terms seem confusing right now. We’ll go through each one (like public
, static
, void
, etc.) in the coming lessons. For now, just understand that this is how it works!
Congratulations! You’ve just written your first Java program and learned how to break it down. Plus, every programmer remembers their first “Hello, World!” moment. So now, go ahead, explore more, and have fun coding in Java!