Here's the simplest Java program that still follows the language's syntax rules:
class Hello {
public static void main(String[] args) {
System.out.println("Hello");
}
}
Why is this the simplest Java program?
Let's break it down:
-
class Hello { ... }
- In Java, everything must be inside a class.
Hello is the class name (matches the file name Hello.java).
- Even if you don't need objects, Java requires a class as the "container" for code.
-
public static void main(String[] args) { ... }
- This is the entry point of the program (where execution starts).
public: Accessible from outside (required by the Java runtime).
static: Runs without creating an instance of the class (simpler).
void: Doesn't return anything (no result needed here).
main: The name Java looks for to start the program.
String[] args: Accepts command-line arguments (but we ignore it here).
-
System.out.println("Hello");
- This prints "Hello" to the console and adds a newline.
System.out is Java's built-in way to output text.
"Hello" is the smallest meaningful message (not empty, not a variable).
Why can't we simplify further?
- No global functions: Java doesn’t allow standalone code outside a class (unlike Python or JavaScript).
- No "script mode": Java requires compilation (
javac Hello.java → Hello.class) before running (java Hello).
- Minimal syntax: Even an "empty" program needs
class + main to satisfy the JVM (Java Virtual Machine).
Proof it's the simplest:
- Remove
class Hello { ... } → Error: Java needs a class.
- Remove
public static void main → Error: No entry point.
- Remove
System.out.println → Program runs but does nothing (not "useful").
- Try
System.out.println(); (empty message) → Compiles, but prints nothing (not meaningful).
Fun fact: In Java 11+, you can write an even quieter version using JShell (Java's REPL):
jshell> System.out.println("Hi")
But that's cheating — JShell hides the class and main boilerplate for you! 😜
For "proper" Java, Hello.java above is the bare minimum:
- 1 class
- 1 method (
main)
- 1 statement (the print)
Compile & run:
javac Hello.java
java Hello
# Output: Hello
You did it! 🎉 Now go tell the world... or just me, I’ll listen. 😊❤️