C Programs Tutorials | IT Developer
IT Developer

Java Programs



Share with a Friend

Introduction to Java

Hello World - Java Program

// HelloWorld.java

public class HelloWorld {

    public static void main(String[] args) {

        System.out.println("Hello, World!");

    }

}

Output

 
OUTPUT :
Hello, World!

How to compile and run

  1. Save the code above into a file named HelloWorld.java.
  2. Open a terminal (or command prompt) and go to the folder with the file.
  3. Compile:

javac HelloWorld.java

This produces HelloWorld.class (the bytecode).

  1. Run:

java HelloWorld

Expected output

Hello, World!

Detailed explanation (line by line)

  • // HelloWorld.java
    A single-line comment — ignored by the compiler. Useful to label the file.
  • public class HelloWorld {
    Declares a public class named HelloWorld. In Java, every application must have at least one class. The source filename must match the public class name (HelloWorld.java).
  • public static void main(String[] args) {
    This is the entry point of the program — the method the Java Virtual Machine (JVM) calls to start your program.
    • public — accessible to the JVM from outside the class.
    • static — no object of the class is needed; JVM can call this method without instantiating HelloWorld.
    • void — the method does not return any value.
    • main — the method name the JVM looks for.
    • String[] args — an array of command-line arguments passed to the program (can be empty).
  • System.out.println("Hello, World!");
    Prints the text Hello, World! to standard output (console), then moves to the next line.
    • System — a core Java class that provides access to system resources.
    • out — a public static field of System (a PrintStream) representing standard output.
    • println(...) — prints the argument, then prints a newline.
  • }
    Closes the main method.
  • }
    Closes the HelloWorld class.

Variations & quick notes

  • Use System.out.print("Hello, World!"); if you don’t want a newline after the text.
  • You can print multiple items: System.out.println("Hello, " + "World!"); — + concatenates strings.
  • Command-line arguments: run java HelloWorld John and inside main args[0] would be "John" (guard against empty args before accessing).