Traditionally the first program in any language prints "Hello, World!". Let's walk through it from creating the file to seeing the output.
Creating and Running via the Command Line
Every Java program goes through the same four steps — from a source file to output on the screen.
Click to walk through the steps ▶
📁
mkdir
Create folder
→
📝
HelloWorld.java
Write code
→
⚙️
javac
Compile
→
▶️
java
Run
→
🖥️
Output
Hello, World!
bash
mkdir hello-java && cd hello-java
# Compile
javac HelloWorld.java
# Run (without .class extension)
java HelloWorld
# Hello, World!Anatomy of the Program
Click on any line to see what it means.
Click a line to see its explanation ▼
java
1
public class HelloWorld {▼
2
public static void main(String[] args) {▼
3
System.out.println("Hello, World!");▼
4
}
▼
5
}
▼
Running Without Compilation (Java 11+)
Starting with Java 11, single-file programs can be run directly — without an explicit compilation step:
bash
java HelloWorld.java
# Hello, World!ℹ️This is convenient for experiments and scripts, but in real projects compilation is always done explicitly via a build system (Maven or Gradle).
Passing Command-Line Arguments
The String[] args parameter in main() receives arguments passed after the class name. Let's explore how args is parsed line by line.
Click a line to see its explanation ▼
java
1
public class Greeting {2
public static void main(String[] args) {3
if (args.length == 0) {▼
4
System.out.println("Hello, stranger!");▼
5
} else {▼
6
System.out.println("Hello, " + args[0] + "!");▼
7
}
▼
8
}
▼
9
}
bash
javac Greeting.java
java Greeting # Hello, stranger!
java Greeting Alice # Hello, Alice!Console Output Methods
Java provides several ways to write to standard output. Click each card to see an example.
Click a method to see how it works
↵
println
→
print
%
printf
✏️
String.format
🚨
System.err
Comments in Java
Java has three types of comments. Click each to see what it looks like and when to use it.
Click a comment type to see an example
//
Single-line
/* */
Multi-line
/**
Javadoc
✅Congratulations — you have written and run your first Java program! The next step is to choose an IDE for a more comfortable development experience. We recommend IntelliJ IDEA Community Edition — the best tool for Java developers.