Hello World in Java

Hello World in Java

Let’s create your first Java program! Open your favorite text editor and type the following code:

Save it as HelloWorld.java

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

Understanding the Code

  • public class HelloWorld declares a class named HelloWorld
  • public static void main(String[] args) is the main method where Java programs start
  • System.out.println() prints text to the console

Compiling and Running

Open your terminal or command prompt and navigate to where you saved the file:

# Compile the Java file
javac HelloWorld.java

# Run the compiled program
java HelloWorld
ℹ️
Notice we run java HelloWorld without the .java extension!

You should see: Hello, World!

Common Issues

Make sure your file name exactly matches your class name. If the class is HelloWorld, the file must be HelloWorld.java.

Learn about variables next to start working with data.

Last updated on