JvJava

Learn Java – tutorial, cheatsheet & exercises

Java has been one of the most important programming languages in the world for almost 30 years. The code is compiled to bytecode and runs on the Java Virtual Machine (JVM) – "Write once, run anywhere". Java is statically typed, object-oriented and, thanks to the garbage collector, memory-safe.

Modern Java (17 to 21 LTS) is much more compact than its reputation suggests: record, var, switch expressions, text blocks, pattern matching, sealed classes and virtual threads remove a lot of boilerplate. Java powers banks, insurance companies, Android apps, big-data systems (Kafka, Spark) and countless backends built with Spring Boot.

Enterprise backendsAndroidBig dataBanking

Hello World in Java

public class Main {
    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

Every Java program needs a class with main. Start it with java Main.java.

How to get started with Java

Install a JDK (Java Development Kit) in version 21 (LTS) – e.g. Temurin from adoptium.net, via brew install openjdk@21 on the Mac or winget install EclipseAdoptium.Temurin.21.JDK on Windows. Check the installation:

java --version
javac --version

Since Java 11 you can run a single source file directly without compiling it first. This is ideal for learning:

java Hallo.java

For larger programs you compile with javac and then start the class (Hallo.java produces Hallo.class):

javac Hallo.java
java Hallo

For quickly trying out individual lines there is the interactive shell jshell (included in the JDK). There you need neither a class nor main:

jshell
jshell> int x = 6 * 7
x ==> 42
jshell> "Java".toUpperCase()
$2 ==> "JAVA"
jshell> /exit
TipFor real projects use an IDE such as IntelliJ IDEA (the Community Edition is free) or VS Code with the "Extension Pack for Java", and a build tool (Maven or Gradle).

Java learning path

Beginner
Intermediate
Advanced

At a glance

First released1995
CreatorJames Gosling (Sun)
ParadigmObject-oriented
TypingStatic, strong
ExecutionCompiled to bytecode, runs on the JVM
File extension.java

Test & practice

Interactive quiz, timed exam with certificate, and exercises with an editor:

Java compared

Hello World: Java comparedVariables: Java comparedConditionals: Java comparedLoops: Java comparedFunctions: Java comparedLists and Arrays: Java comparedDictionaries and Maps: Java comparedClasses and Objects: Java comparedError Handling: Java comparedReading Files: Java compared