Java First Program (Hello World)
Writing the first Java program is a rite of passage for every learner. The classic "Hello, World!" program is simple — it displays a message on the screen. But even this short program teaches the fundamental structure of every Java application.
The Hello World Program
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}When this program runs, it displays the following output:
Hello, World!Understanding Every Line
Every part of this program has a specific role. Let's break it down step by step.
Line 1: public class HelloWorld
public class HelloWorld {- public: This is an access modifier. It means this class is accessible from anywhere.
- class: This keyword is used to define a class. In Java, every program must have at least one class.
- HelloWorld: This is the name of the class. The class name must exactly match the file name. Since the file is named
HelloWorld.java, the class is also namedHelloWorld. - { } (curly braces): These define the start and end of the class body — everything inside belongs to this class.
Line 2: public static void main(String[] args)
public static void main(String[] args) {This is the main method. Every Java program must have this exact method — it is the entry point from where the program starts running.
- public: The method is accessible from anywhere, including the JVM.
- static: The method belongs to the class itself, not to any specific object. The JVM calls it without creating an object first.
- void: This method does not return any value after it finishes.
- main: This is the name of the method. The JVM looks specifically for a method called
mainto start the program. - String[] args: This allows the program to accept input from the command line when it starts. It is not used in this example, but it must still be there.
Line 3: System.out.println("Hello, World!");
System.out.println("Hello, World!");- System: A built-in Java class that provides access to system resources.
- out: A part of the System class that represents the output stream (the screen).
- println: Short for "print line." It prints the given text to the screen and moves the cursor to the next line.
- "Hello, World!": The text inside double quotes is called a String — a sequence of characters to be printed.
- ; (semicolon): Every statement in Java must end with a semicolon.
How to Create and Run This Program
Using the Command Line
- Open a text editor (Notepad, gedit, or any editor).
- Type or paste the Hello World code.
- Save the file as HelloWorld.java (the file name must match the class name exactly, including capitalization).
- Open the terminal or command prompt.
- Navigate to the folder where the file is saved using the
cdcommand:cd path/to/folder - Compile the program:
This creates ajavac HelloWorld.javaHelloWorld.classfile containing the bytecode. - Run the program:
java HelloWorld - The output appears:
Hello, World!
Using an IDE (IntelliJ IDEA)
- Open IntelliJ IDEA and create a new Java project.
- Inside the project, create a new Java class named
HelloWorld. - Type the Hello World code into the class.
- Click the green Run button or press Shift + F10.
- The output appears in the console at the bottom of the screen.
Common Mistakes to Avoid
| Mistake | Effect | Correct Form |
|---|---|---|
| File name does not match class name | Compilation error | HelloWorld.java → public class HelloWorld |
| Missing semicolon at end of statement | Compilation error | System.out.println("Hello"); |
Wrong capitalization in System, String | Compilation error | System.out.println (capital S) |
| Missing curly braces | Compilation error | Every opening { must have a matching closing } |
print vs println
Java provides two methods for printing output:
- System.out.println(): Prints the text and moves to the next line.
- System.out.print(): Prints the text and stays on the same line.
public class PrintExample {
public static void main(String[] args) {
System.out.print("Hello ");
System.out.print("World");
System.out.println();
System.out.println("New Line starts here");
}
}Output:
Hello World
New Line starts hereSummary
- Every Java program must have a class and a
mainmethod. - The file name must match the public class name exactly.
- The
mainmethod is where program execution begins. System.out.println()is used to print output to the screen.- Every statement in Java ends with a semicolon
;. - Java is case-sensitive —
Mainandmainare treated as different names.
