Hello World

Hello World

A school program needs reliable data and clear rules. A first program connects source code, compilation and execution. The main method below is the entry point for this Java 17 example; println writes a line to standard output.

  1. Understand

    A first program connects source code, compilation and execution.

  2. Apply

    Trace the Java example, predict its output and explain the result before running it.

  3. Avoid mistakes

    Save the public class as Main.java. Java identifiers are case-sensitive, so Main, main and println have different roles.

Writing Your First Java Program

Create a file named HelloWorld.java. Java is strict about naming — your filename must match the class name if it's public. HelloWorld in HelloWorld.java should be same as HelloWorld in public class HelloWorld.

public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}
Hello, World!

Compiling the Program

Open a terminal or command prompt where your HelloWorld.java file is saved and run the following command:

javac HelloWorld.java

What happens during compilation?

The javac command invokes the Java compiler. Here's what it does behind the scenes:

  • It reads your .java source file — which contains human-readable Java code.
  • Then it checks for syntax errors or code issues.
  • If everything is correct, it converts the source code into bytecode — a platform-independent intermediate form of the code.
  • This bytecode is saved in a new file named HelloWorld.class.

This bytecode is what the Java Virtual Machine (JVM) understands and executes — not the original source code.

Running the Program

Java · example discussed here

public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}

Now, execute the compiled bytecode using the java command:

java HelloWorld

What happens during execution?

The java command calls the JVM (Java Virtual Machine) to:

  • Locate the HelloWorld.class file.
  • Load the bytecode into memory.
  • Interpret or Just-In-Time (JIT) compile it into native machine code specific to your system.
  • Execute the main method of the class.
Hello, World!

You’ve just compiled and executed your first Java program. This process — compile once, run anywhere — is what makes Java so powerful and portable.

Common Issues to Watch For

  • Filename mismatch: Ensure your filename is HelloWorld.java if your class is HelloWorld.
  • Missing main method: Java won’t run if it doesn’t find the exact public static void main method.
  • Case sensitivity: Java is case-sensitive. Main and main are not the same.

Important boundaries and mistakes

Save the public class as Main.java. Java identifiers are case-sensitive, so Main, main and println have different roles.

Try the school example

Before running this Java program, write down each line you expect to see. Identify the value, branch, object or exception responsible for each line. Then explain whether the following assumption is valid: Save the public class as Main.java.

Java 17 · Main.java · complete practice program

public class Main  {
  public static void main(String[] args)  {
    System.out.println("Welcome to school");
  }
}

Worked answer and reasoning

Java 17 · Main.java · source repeated for this answer

public class Main  {
  public static void main(String[] args)  {
    System.out.println("Welcome to school");
  }
}

Expected output

Welcome to school

A first program connects source code, compilation and execution. The main method below is the entry point for this Java 17 example; println writes a line to standard output.

Save the public class as Main.java. Java identifiers are case-sensitive, so Main, main and println have different roles.

Run with javac Main.java, then java Main. The declaration of Main names the class; main starts the example, and println writes a complete output line.

Check your understanding

For the complete practice program in this lesson, which output is expected on Java 17?
Choose one answer. You can try again.

Answer and explanation

Welcome to school

A first program connects source code, compilation and execution. The main method below is the entry point for this Java 17 example; println writes a line to standard output. Save the public class as Main.java. Java identifiers are case-sensitive, so Main, main and println have different roles.

What to remember

A first program connects source code, compilation and execution. The main method below is the entry point for this Java 17 example; println writes a line to standard output. Save the public class as Main.java. Java identifiers are case-sensitive, so Main, main and println have different roles.

Related explanations: Java Variables, Java If Statement - Syntax and Examples, Java Class - Blueprint of Objects, Exceptions in Java Introduction.

Supporting documentation: Official Java learning guide and Java 17 language specification.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Comments →