Java Tutorial

This Java tutorial takes you from your very first line of code to writing real object-oriented programs. Java is a general-purpose, class-based programming language created at Sun Microsystems in 1995 and maintained today by Oracle and the OpenJDK community.

Java code is compiled into bytecode, which runs on the Java Virtual Machine (JVM). Because every major operating system has a JVM, the same compiled program runs on Windows, macOS and Linux without changes. That idea is often summarised as "write once, run anywhere".

All examples in this course target Java 21, the current long-term support (LTS) release, so you will learn modern features such as var, records, switch expressions, text blocks and pattern matching from the start.

Why Learn Java?

  • Java powers huge back-end systems at banks, airlines, e-commerce sites and governments.
  • Android apps were built in Java for years, and Kotlin still runs on the same JVM.
  • Frameworks like Spring Boot make Java one of the most in-demand skills for back-end jobs.
  • It is strongly typed, so the compiler catches many mistakes before your program runs.
  • It teaches object-oriented programming in a clear way that transfers to C#, Kotlin and TypeScript.
  • It is free, open source and has a massive community and library ecosystem.

Your First Java Program

Every Java program lives inside a class, and execution starts in a special method called main. The program below prints a greeting to the console.

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

Do not worry about every keyword yet. public class Main declares a class named Main, public static void main(String[] args) is the entry point the JVM looks for, and System.out.println prints a line of text. The Java Syntax lesson explains each part in detail.

What This Java Tutorial Covers

  • Getting started: installing the JDK and running programs
  • Syntax, comments, output, variables and data types
  • Operators, strings, conditions, switch and loops
  • Arrays and methods
  • Object-oriented programming: classes, objects, constructors, inheritance, interfaces
  • Exceptions, collections (ArrayList, HashMap), generics
  • Modern Java: lambdas, streams, records, enums and pattern matching
  • A final lesson with complete example programs

A Slightly Bigger Example

Here is a taste of modern Java. It stores some numbers in a list and uses a stream to add up the even ones. You will understand every line by the end of the course.

import java.util.List;

public class Main {
    public static void main(String[] args) {
        var numbers = List.of(3, 8, 5, 12, 7, 10);
        int sumOfEvens = numbers.stream()
                .filter(n -> n % 2 == 0)
                .mapToInt(Integer::intValue)
                .sum();
        System.out.println("Sum of even numbers: " + sumOfEvens);
    }
}
Sum of even numbers: 30

How to Use This Tutorial

Read the lessons in order, because each one builds on the last. Type the examples yourself rather than copying them; making and fixing small mistakes is how you really learn. Each example shows the exact output you should see, so you can check your work.

Frequently asked questions

Is Java hard to learn for beginners?

Java is a little more verbose than Python, but its rules are consistent and the compiler gives clear error messages. Most beginners can write useful programs within a few weeks of steady practice.

What is Java used for?

Java is used for back-end web services, enterprise applications, Android apps, big data tools like Hadoop and Kafka, and desktop software. It is especially common in large companies.

Is Java the same as JavaScript?

No. Despite the similar names, they are different languages. Java is compiled and statically typed and runs on the JVM, while JavaScript is a dynamic language that mainly runs in browsers and Node.js.

Which Java version should I learn?

Learn Java 21, the current long-term support release. Code written for older versions still runs, and you get modern features like records and pattern matching.