Learn how to write and run your first Java program with a simple HelloWorld example. Step-by-step explanation of code structure, compilation, JVM execution, and beginner tips.
HelloWorld.javaIn Java, every piece of code must live inside a class. Here is what the code looks like:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
Don’t worry if this looks like a secret code at first. Here is what each part actually does:
public class HelloWorld: This defines the “bucket” for your code. In Java, the filename must match the class name (so this file must be saved as HelloWorld.java).public static void main(String[] args): This is the entry point. It’s the door the computer walks through to start running your program. Without this specific line, your program won’t run.System.out.println: This is the command that tells the computer to “print” or display text on the screen.;): Think of this as a period at the end of a sentence. It tells Java the instruction is finished.Unlike some languages that run directly, Java uses a two-step process to ensure it can run on any device (Windows, Mac, or Linux).
javac), which turns your readable text into “Bytecode.”If you have the Java Development Kit (JDK) installed, you can try this right now:
HelloWorld.java.javac HelloWorld.javajava HelloWorld