Java is a general-purpose programming language created in the 1990s and still one of the most used today. It powers Android apps, bank and airline systems, online shops and many of the services behind large websites.
Java code is written in files ending in .java. Before it runs, a tool called the compiler checks the whole file and translates it into bytecode. The Java Virtual Machine (JVM) then runs that bytecode on Windows, macOS, Linux or a phone. That's how one Java program can run almost anywhere.
In this lesson you'll install Java, look at every part of the classic Hello World program, and learn how to show output and write comments.
Installing the JDK
To write Java you need the JDK (Java Development Kit), which contains the compiler and the JVM.
- Download a current LTS version (21 or newer) of Eclipse Temurin from adoptium.net, or the JDK from oracle.com.
- Run the installer and accept the option to set
JAVA_HOMEand the PATH. - Open a terminal and check it worked:
java --versionYou should see a version number such as openjdk 21.0.4. An editor such as IntelliJ IDEA Community or VS Code with the Java extension will highlight errors as you type and add a Run button.
The parts of a Java program
public class Main {
public static void main(String[] args) {
System.out.println("Hello, world!");
}
}public class Main: every Java program lives inside a class. The file must have the same name as the public class:Main.java.public static void main(String[] args): the main method is where the program starts. Java looks for exactly this line.System.out.println(...): prints a line of text.- Curly braces
{ }mark where the class and the method begin and end. - Every statement ends with a semicolon
;.
Don't worry about memorising public static void yet. You'll understand each word by the end of this course.
Compiling and running
From a terminal in the folder containing Main.java:
javac Main.java
java Mainjavac compiles the code into Main.class, and java Main runs it. Since Java 11 you can also run a single file directly with java Main.java, which is handy while learning. Editors do these steps for you when you press Run.
How Java code runs: bytecode and the JVM
Java sits between fully compiled languages such as C and interpreted languages such as Python. javac doesn't produce machine code for your processor. It produces bytecode, a compact set of instructions for the Java Virtual Machine, stored in .class files.
When you run java Main, the JVM loads that bytecode, checks it is safe to run, and starts executing it. Code that runs often is compiled into fast native machine code while the program is running, by the just-in-time (JIT) compiler. That's why long-running Java servers become very fast once they have warmed up.
Three names come up constantly:
| Name | What it is |
|---|---|
| JVM | The virtual machine that runs bytecode |
| JRE | The JVM plus the standard library: enough to run Java programs |
| JDK | The JRE plus developer tools such as javac, jshell and javadoc |
As a learner you always want the JDK. The JDK also includes jshell, an interactive prompt where you can type a line such as 2 + 3 * 4 and see the result immediately, without writing a class. The JDK, JRE and JVM lesson looks at each part in more detail.
Java versions and LTS releases
A new Java version is released every six months, and every two years one of them is a long-term support (LTS) release that receives updates for many years. Java 17, 21 and 25 are LTS releases. Most companies run an LTS version, so learning on 21 or newer is a safe choice. Code from older versions almost always keeps working, because Java takes backwards compatibility very seriously.
println, print and comments
System.out.println(x)printsxand then moves to a new line.System.out.print(x)printsxand stays on the same line.- Use
+to join text and values:"Total: " + 5.
Comments are notes that Java ignores:
// A single-line comment
/* A comment that can
span several lines */Java Syntax
A complete Java program
public class Main {
public static void main(String[] args) {
// your code here
System.out.println("text");
}
}Java Summary
- Install the JDK to get the Java compiler and runtime.
- Every Java program is a class, and it starts running in the
mainmethod. - The file name must match the public class name:
Main.java. - Statements end with
;and blocks are wrapped in{ }. printlnprints a line;printstays on the same line;//starts a comment.
Java Examples
Each example shows the code, the output it produces and what each part does.
Hello, world
public class Main {
public static void main(String[] args) {
System.out.println("Hello, world!");
System.out.println("I'm learning Java.");
}
}Hello, world!
I'm learning Java.Line-by-line explanation
- Line 1The class is called Main, so the file must be Main.java.
- Line 2The main method: the program's starting point.
- Lines 3 to 4Each println prints one line.
print versus println
public class Main {
public static void main(String[] args) {
System.out.print("Loading");
System.out.print("...");
System.out.println(" done!");
System.out.println("Next line");
}
}Loading... done!
Next lineLine-by-line explanation
- Lines 3 to 4print keeps the cursor on the same line.
- Line 5println ends the line, so the next output starts below.
Joining text and numbers
public class Main {
public static void main(String[] args) {
// Java can do maths inside println
System.out.println("Tickets: " + 3);
System.out.println("Cost: " + (3 * 12));
System.out.println("7 / 2 = " + (7 / 2));
System.out.println("7.0 / 2 = " + (7.0 / 2));
}
}Tickets: 3
Cost: 36
7 / 2 = 3
7.0 / 2 = 3.5Line-by-line explanation
- Line 3A comment explaining the code. Java skips it.
- Line 5Brackets make the maths happen before joining with the text.
- Line 6Dividing two whole numbers drops the decimal part.
- Line 7With a decimal number involved, you get a decimal answer.
Common Java Mistakes
Missing semicolon
System.out.println("Hi")System.out.println("Hi");The compiler reports "';' expected" and points to the line.
File name doesn't match the class
// File: hello.java
public class Main {
public static void main(String[] args) { }
}// File: Main.java
public class Main {
public static void main(String[] args) { }
}A public class must be saved in a file with exactly the same name.
Forgetting brackets around maths
System.out.println("Sum: " + 2 + 3); // Sum: 23System.out.println("Sum: " + (2 + 3)); // Sum: 5Once text is involved, + joins from left to right, so 2 and 3 are joined as text.
Java in Real Projects
Where Java runs
Android's own apps and many of the apps on the Play Store are written in Java or its close relative Kotlin. Banks, airlines and online shops run Java on their servers, and Minecraft Java Edition shows that it's used for games too. In every one of them, the program starts in a main method just like yours.
Java FAQ
Is Java the same as JavaScript?
No. Despite the similar names they are different languages with different designs. Java is compiled to bytecode and runs on the JVM, with static types checked before the program runs. JavaScript mostly runs in web browsers and in Node.js, and its types are checked while the code runs. The name similarity was a marketing decision in the 1990s.
Is Java hard to learn for beginners?
Java asks for a little more typing at the start: classes, main and declared types. In return, the compiler catches many mistakes before the program runs and explains where they are. Beginners who practise a little every day can write useful console programs within a few weeks.
Which Java version should I learn?
Learn a current long-term support release, such as Java 21 or 25. Everything in this course works on Java 21, and the core language you learn is the same in older and newer versions.
Do I need an IDE to write Java?
No. A text editor and the javac and java commands are enough, and they help you understand what happens behind the scenes. An IDE such as IntelliJ IDEA or VS Code with the Java extension becomes very helpful as programs grow, because it highlights errors, completes code and runs programs with one click.
Is Java free to use?
Yes. OpenJDK is open source, and builds such as Eclipse Temurin can be used free of charge for learning, personal projects and commercial work. Oracle also publishes its own JDK builds under its own licence terms, so check those if your employer uses them.
What is Java used for today?
Java is used for backend web services, banking and insurance systems, Android apps, big data tools such as Apache Kafka and Hadoop, and desktop and scientific software. Many large companies run important systems written in Java, which keeps demand for Java developers steady.
Java Exercises
Answer each one and check it instantly. If you get stuck, open the hint before the answer.
Complete the line so it prints Hi and moves to a new line.
System.out.("Hi");
What does this print?
System.out.print("A");
System.out.print("B");
System.out.println("C");
What does this print?
System.out.println("Result: " + 4 + 2);
Where does a Java program start running?
Which line prints text and moves to a new line?
Java Quiz
5 questions to check what you learned.
What must every Java statement end with?
- A colon
- A full stop
- Nothing
- A semicolon
Which tool turns a .java file into bytecode?
- java
- javac
- jvm
- jar
If your file is called
Game.java, what must the public class be called?- Game
- Main
- game
- Any name
What does
7 / 2give in Java?- 3.5
- 4
- 3
- An error
What must the file containing
public class Mainbe called?- Main.java
- main.java
- Main.class
- Any name