Lesson 1 of 13
Hello, World!
Write and run your first Java program. You will see why even a small program needs a and a main , and how to compile and run it.
In Java, all code must be inside a class. The entry point is a static method called main. The full signature — public static void main([] args) — is required exactly.
// File: HelloWorld.java
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}Compile with javac HelloWorld.java, then run with java HelloWorld. Java 11+ added the ability to run single-file programs directly: java HelloWorld.java. The JVM (Java Virtual Machine) runs the compiled bytecode — this is the "run anywhere" part.
public class HelloWorld {
public static void main(String[] args) {
// println adds newline; print does not
System.out.println("Hello, World!");
System.out.print("No newline");
System.out.print(" here
");
// Formatted output (like printf)
System.out.printf("Name: %s, Age: %d%n", "Alice", 30);
// String.format — returns a string
String msg = String.format("Pi is %.4f", 3.14159);
System.out.println(msg);
// Command-line arguments
if (args.length > 0) {
System.out.println("First arg: " + args[0]);
}
// Modern Java (21): text blocks
String json = """
{
"name": "Alice",
"age": 30
}
""";
System.out.println(json);
}
}Finished reading?Mark it done to track your progress.
Tip: use the ← and → keys to move between lessons.