Java Tutorial
Java is one of the most used programming languages in the world. It runs the server side of banks, shops and big websites, almost every Android app is written in it or in a close relative, and many schools and universities teach it as the first "real" language. Java code is clear and strict: you say exactly what type each value has, and the compiler checks your program before it runs, so many mistakes are caught early.
This tutorial takes you from your very first program to classes, collections, streams, files and threads. Each topic explains one idea in plain words and shows short example programs. Every example has a Try it in the editor button: it opens the program in our online Java editor in a new tab, where you can change it, run it and see what happens. Nothing needs to be installed: a real Java 23 compiler and Java virtual machine run inside your browser.
How this tutorial is organised
The topics follow the order most courses use, and each one builds on the ones before it. When a topic uses an idea from an earlier one, it links back to it, so you can always go back and read it again.
- Basics: the shape of a program, output, comments, variables, data types, operators, strings, the Math class, booleans, user input, decisions and loops.
- Arrays and methods: arrays, the Arrays class, two-dimensional arrays, writing your own methods, parameters, overloading, scope and recursion.
- Classes and objects: classes, fields, constructors, access modifiers, encapsulation, packages, inheritance, polymorphism, abstract classes, interfaces, inner classes, enums and records.
- Errors and collections: exceptions, ArrayList, LinkedList, HashMap, HashSet, iterators and generics.
- Modern Java and more: lambda expressions, streams, dates and times, random numbers, files, regular expressions, threads and debugging.
Use the topic list to jump anywhere, and the Previous and Next buttons to read the course in order.
Your first look at Java
Here is a complete Java program. Press Try it in the editor, then Run, and the terminal shows its output.
public class Main {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}Hello, World!
Compared with some other languages there is more around the one line that prints: a class called Main and a method called main. Every Java program has this shape. The next topic, Get Started, explains each word of it, and after a few topics you will write it without thinking.
Tips for learning Java
- Type the examples yourself. Reading code is not the same as writing it. Small typing mistakes teach you to read the compiler's messages.
- Change things and run again. Change a number, remove a semicolon, print one more line. Predict what will happen first, then check.
- Read the error messages. The Java compiler tells you the file, the line and what it expected. Our editor marks the line and adds a hint in plain words.
- Keep the class name and the file name the same. A public class called
Mainlives in a file calledMain.java. The editor does this for you.
Practice exercises
- Run the example above, then change the text so it prints your own name.
- Add a second
System.out.printlnline that prints the name of your city. - Remove the semicolon at the end of the
printlnline, run the program and read the error and the hint. Put the semicolon back.