Your First Java Program

Site Admin · 11 Sep 2026 · 14 views

Hello, World

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

Save, Compile, Run

  1. Save the file as HelloWorld.java. The file name must match the public class name exactly.
  2. Compile with javac HelloWorld.java. This produces HelloWorld.class containing bytecode.
  3. Run with java HelloWorld (without the .class extension). The output is Hello, world!

Understanding Each Line

  • public class HelloWorld: declares a public class named HelloWorld. In Java the top-level structure of a program is a class.
  • public static void main(String[] args): the entry point. public means the JVM may call it, static means it belongs to the class rather than an object, and void means it returns nothing. args holds any command-line arguments passed to the program.
  • System.out.println(...): prints the given text followed by a new line to the console.
  • Passing Arguments

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

    Run it as java Greet Priya and the program prints Hello, Priya!

    First Java program output

    Key Points

    • The filename must equal the public class name.
    • Compile with javac, run with java.
    • main is where the JVM begins executing your program.
Share this post:

Comments (0)

Please login or register to comment.