Hello Java
Java is one of the most widely used programming languages in the world. Banks, airlines, online shops, Android apps, big-data systems like Kafka and Spark, and Minecraft all run on Java. It's strongly typed, object-oriented, and famous for one idea: write once, run anywhere.
How Java runs
Java code isn't run directly. It goes through two steps:
Main.java ──javac──▶ Main.class (bytecode) ──java──▶ runs on the JVM
(you write) (portable, any OS) (Windows, macOS, Linux…)- The compiler (
javac) checks your code and turns it into bytecode. - The Java Virtual Machine (JVM) runs that bytecode on any operating system, optimising hot code into fast machine code as it runs.
Because the compiler checks types before the program runs, many mistakes are caught early: a big reason companies trust Java for large systems.
💡 About the examples in this track: Java can't run inside a web page the way Python and JavaScript can, so every example shows its Output underneath. Every program in this track was compiled and run with Java 25 to produce that output exactly. Copy any example (there's a Copy button) and run it yourself.
Your first program
public class Main {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}Hello, World!
Every part has a job:
| Code | Meaning |
|---|---|
public class Main |
All Java code lives inside a class. The file must be named Main.java. |
public static void main(String[] args) |
The entry point: where the program starts. |
System.out.println(...) |
Print a line of text. |
{ } |
Mark the start and end of blocks. |
; |
Ends every statement. |
It looks like a lot of ceremony for "Hello". You'll understand every word (public, static, void, String[]) by the end of the classes lessons. For now, think of it as the frame every program sits in.
Printing
public class Main {
public static void main(String[] args) {
System.out.println("Line one");
System.out.print("No newline after this. ");
System.out.print("Same line.\n");
System.out.println("6 x 7 = " + (6 * 7));
System.out.printf("%s is %d years old and %.1f m tall%n", "Asha", 31, 1.68);
// This is a comment: the compiler ignores it
/* So is this,
across several lines */
}
}Line one No newline after this. Same line. 6 x 7 = 42 Asha is 31 years old and 1.7 m tall
printlnprints and moves to a new line;printdoesn't.+joins text and values.printfformats values:%stext,%dwhole number,%.1fa decimal with 1 place,%nnewline.
Compile errors are your friend
Forget a semicolon and the compiler refuses to build the program, pointing at the exact spot:
public class Main {
public static void main(String[] args) {
System.out.println("Hello")
}
}Main.java:3: error: ';' expected
System.out.println("Hello")
^
1 error
error: compilation failedIn languages like Python and JavaScript, many mistakes only show up when that line runs. Java catches them before the program starts.
Installing Java
- Install a JDK (Java Development Kit). Pick a long-term support version: Java 25, 21 or 17. Free builds: Eclipse Temurin (adoptium.net), Microsoft, Amazon Corretto, Oracle OpenJDK.
- macOS:
brew install --cask temurin - Windows: the installer from adoptium.net, or
winget install EclipseAdoptium.Temurin.25.JDK - Linux:
sudo apt install openjdk-25-jdk(or the newest your distribution offers)
- macOS:
- Check it in a terminal:
java -version
javac -version- Run a program:
javac Main.java # compile → Main.class
java Main # run
java Main.java # or compile and run in one step (Java 11+)- Use an IDE: IntelliJ IDEA (Community edition is free; the most popular Java IDE), VS Code with the "Extension Pack for Java", or Eclipse. They compile as you type and underline errors instantly.
A modern shortcut (Java 25)
Since Java 25, small programs can skip the class and the long main signature. This is great for learning and scripts:
void main() {
IO.println("Hello from compact Java!");
String name = "Asha";
IO.println("Hi " + name);
}Hello from compact Java! Hi Asha
Most real code (and the rest of this track) uses the classic form, because you'll see it everywhere: in every company codebase, tutorial and framework.
Java vs JavaScript
Despite the name, they're unrelated languages. The name was a 1990s marketing decision.
| Java | JavaScript | |
|---|---|---|
| Typing | Static (types checked when compiling) | Dynamic |
| Runs on | The JVM | Browsers, Node.js |
| Typical use | Backends, Android, enterprise, big data | Websites, web apps, Node backends |
Practice
On your computer (or any online Java compiler), write a program that prints:
- Your name on one line.
- Your age next year, calculated with
+. - A formatted line with
printf:"Coffee costs ₹120.00".
1.What does javac do?
2.Why can the same compiled Java program run on Windows, macOS and Linux?
3.What must the file containing public class Main be called?