Back to course

Your First Java Program: 'Hello World' Explained

Java Mastery: From Zero to Professional Developer (50-Lesson Journey)

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

TermDescription
publicAn access modifier. It means this class/method is accessible from everywhere.
classKeyword used to define a class. The class name must match the filename (HelloWorld).
staticAllows the main method to be called without creating an instance of the class.
voidSpecifies that the method does not return any value.
mainThe entry point for the execution of any Java application. The JVM starts here.
String[] argsAn 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)

  1. Compile: Turns your .java source code into platform-independent bytecode (.class file). bash javac HelloWorld.java

  2. Run: Executes the bytecode using the JVM. bash java HelloWorld

Output:

Hello, World!