Start Coding Now

First Java Program - Hello World Example

Writes your first Java program. Understand the class structure, main method, and how to compile and run Java code.

Your First Java Program

Let's write the traditional "Hello, World!" program.

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

Breaking it Down

  • class HelloWorld: Defines a class named HelloWorld. In Java, every line of code must run inside a class.
  • public static void main: The entry point. The JVM looks for this method to start execution.
  • System.out.println: Prints text to the console.
  • How to Run

  • Save file as HelloWorld.java.
  • Open terminal/command prompt.
  • Compile: javac HelloWorld.java (Creates HelloWorld.class).
  • Run: java HelloWorld (Outputs: Hello, World!).
  • Frequently Asked Questions

    What is the main method?

    public static void main(String[] args) is the entry point of any Java application.