Lesson 3: Your First Java Program: 'Hello World' Explained
Let's write the canonical first program.
The Code
Create a file named HelloWorld.java:
java public class HelloWorld { public static void main(String[] args) { // This line prints the message to the console System.out.println("Hello, World!"); } }
Deconstructing the Code
| Term | Description |
|---|---|
public | An access modifier. It means this class/method is accessible from everywhere. |
class | Keyword used to define a class. The class name must match the filename (HelloWorld). |
static | Allows the main method to be called without creating an instance of the class. |
void | Specifies that the method does not return any value. |
main | The entry point for the execution of any Java application. The JVM starts here. |
String[] args | An array of strings used to pass command-line arguments to the application. |
System.out.println() | A standard method used to output text to the console (standard output). |
Execution (Command Line)
-
Compile: Turns your
.javasource code into platform-independent bytecode (.classfile). bash javac HelloWorld.java -
Run: Executes the bytecode using the JVM. bash java HelloWorld
Output:
Hello, World!