Learn Java

Statically typed, object-oriented language for the JVM, from backends to Android

42 lessons · 9 hours · 4 levels · covers Java 21

Java is a statically typed, object-oriented language that compiles to bytecode for the Java Virtual Machine, so the same program runs on any platform. It powers a huge share of backend services, big-data systems and Android apps, with a mature ecosystem of libraries, frameworks and tools. Learning it teaches you solid type-driven design and gives you access to one of the most in-demand platforms in industry.

How deep do you want to go?

1. EssentialsRead and write simple code, do the everyday basics12 lessons · 2 hours in total
2. PracticalUse it productively at work, with the usual idioms27 lessons · 5 hours in total
3. AdvancedThe less common features, performance and trade-offs36 lessons · 7 hours in total
4. ExpertInternals and the edge cases experts know42 lessons · 9 hours in total

The Java tree

JavaInstall the JDK and run codeInstall the JDK andrun codeAnatomy of a Java programAnatomy of a JavaprogramVariables and primitive typesVariables andprimitive typesOperators and expressionsOperators andexpressionsStrings and text blocksStrings and textblocksif, else and switchif, else and switchLoops: for, while and for-eachLoops: for, while andfor-eachArraysArraysMethodsMethodsWrapper types and boxingWrapper types andboxingConsole input and formatted outputConsole input andformatted outputClasses and objectsClasses and objectsConstructors and object setupConstructors andobject setupEncapsulation and access modifiersEncapsulation andaccess modifiersInheritance and polymorphismInheritance andpolymorphismInterfacesInterfacesEnumsEnumsRecordsRecordsExceptionsExceptionsGenerics basicsGenerics basicsLists and the Collections FrameworkLists and theCollections Framew…Maps, Sets and equals/hashCodeMaps, Sets andequals/hashCodeLambdas and functional interfacesLambdas andfunctional interfacesStreamsStreamsOptionalOptionalPackages, Maven and GradlePackages, Mavenand GradleUnit testing with JUnit 5Unit testing withJUnit 5
Essentials and Practical shown (27 of 42 lessons); the full list is below. Click a lesson to read it.

All 42 lessons, in order

Language basics

  1. Install the JDK and run code · Essentials · 10 min

    Install a JDK, then compile with javac and run with java — or launch a single source file directly.

  2. Anatomy of a Java program · Essentials · 8 min

    Classes, the main method, statements, comments and printing output — what every line of Hello World means.

  3. Variables and primitive types · Essentials · 12 min

    Declare variables, pick among the eight primitive types, write literals and use var for local type inference.

  4. Operators and expressions · Essentials · 10 min

    Arithmetic, integer division, increment, comparison, logical and ternary operators, plus casting between numeric types.

  5. Strings and text blocks · Essentials · 12 min

    Work with immutable Strings: compare with equals, use common methods, build text with StringBuilder and text blocks.

  6. if, else and switch · Essentials · 10 min

    Branch with if/else and with modern switch expressions that return a value and never fall through.

  7. Loops: for, while and for-each · Essentials · 10 min

    Repeat work with for, while, do-while and the enhanced for-each loop, and control it with break and continue.

  8. Arrays · Essentials · 10 min

    Create fixed-size arrays, index into them, loop over them and use java.util.Arrays helpers to print, sort and copy.

  9. Methods · Essentials · 12 min

    Define static methods with parameters and return values, overload them, and understand that Java always passes by value.

  10. Wrapper types and boxing · Essentials · 8 min

    Integer, Double and friends wrap primitives as objects; learn autoboxing, parsing text to numbers and the null and == traps.

  11. Console input and formatted output · Essentials · 8 min

    Read user input with Scanner or BufferedReader and print neatly with printf and format specifiers.

Classes and types

  1. Classes and objects · Essentials · 14 min

    Define a class with fields and instance methods, create objects with new, and understand references, this and null.

  2. Constructors and object setup · Practical · 10 min

    Initialise objects with constructors, overload them, chain with this(...), and validate arguments up front.

  3. Encapsulation and access modifiers · Practical · 10 min

    Hide state behind private fields, expose behaviour through methods, and choose between public, protected, package-private and private.

  4. Inheritance and polymorphism · Practical · 14 min

    Extend classes, override methods, call super, and let polymorphism pick the right method at runtime.

  5. Interfaces · Practical · 12 min

    Define contracts with interfaces, implement several at once, and use default and static methods.

  6. Enums · Practical · 10 min

    Model fixed sets of constants with enums that can carry fields, constructors and methods, and switch over them.

  7. Records · Practical · 10 min

    Declare immutable data carriers in one line; records generate the constructor, accessors, equals, hashCode and toString.

  8. Sealed types and pattern matching · Advanced · 15 min

    Model closed hierarchies with sealed interfaces and records, then destructure them with instanceof and switch patterns.

Errors and files

  1. Exceptions · Practical · 14 min

    Throw and catch exceptions, understand checked vs unchecked, and clean up resources with try-with-resources.

  2. Files and I/O with NIO.2 · Advanced · 12 min

    Read and write files with Path and Files, stream large files line by line, walk directories, and handle charsets correctly.

Collections and generics

  1. Generics basics · Practical · 10 min

    Use type parameters like List<String> and Map<K, V>, write simple generic classes and methods, and let the diamond infer types.

  2. Lists and the Collections Framework · Practical · 12 min

    Use List, ArrayList and List.of, iterate and sort them, and know when to pick which list implementation.

  3. Maps, Sets and equals/hashCode · Practical · 14 min

    Store key-value pairs in HashMap, unique items in HashSet, and implement equals and hashCode correctly for your keys.

  4. Generics in depth: wildcards and erasure · Advanced · 15 min

    Use bounded wildcards with the PECS rule, understand invariance and type erasure, and know what generics can't do at runtime.

Functional style

  1. Lambdas and functional interfaces · Practical · 12 min

    Pass behaviour as values with lambdas and method references, and use the standard functional interfaces in java.util.function.

  2. Streams · Practical · 15 min

    Process collections declaratively with filter, map, sorted, collect, groupingBy and friends.

  3. Optional · Practical · 8 min

    Return Optional for values that may be absent and handle them with map, orElse, orElseThrow and ifPresent.

Build and test

  1. Packages, Maven and Gradle · Practical · 14 min

    Organise code into packages, understand the classpath and JARs, and build projects with Maven or Gradle.

  2. Unit testing with JUnit 5 · Practical · 14 min

    Write and run JUnit 5 tests with assertions, assertThrows, lifecycle hooks and parameterized tests.

Concurrency

  1. Threads and thread safety · Advanced · 15 min

    Start threads, recognise race conditions, and fix them with synchronized, locks, atomics and concurrent collections.

  2. Executors and thread pools · Advanced · 12 min

    Submit tasks to an ExecutorService, collect results with Future, and shut pools down correctly.

  3. CompletableFuture · Advanced · 14 min

    Compose asynchronous steps with thenApply, thenCompose, thenCombine, allOf and exceptionally instead of blocking on futures.

  4. Virtual threads · Advanced · 12 min

    Use Java 21 virtual threads to run huge numbers of blocking tasks cheaply, and know their pitfalls: pinning, pooling and ThreadLocals.

Platform features

  1. Annotations and reflection · Advanced · 14 min

    Declare and read custom annotations, inspect and invoke code at runtime with reflection, and know the costs.

  2. The module system (JPMS) · Advanced · 12 min

    Describe modules with module-info.java: requires, exports, opens and transitive, and build small runtimes with jlink.

JVM internals

  1. Java Memory Model and happens-before · Expert · 18 min

    Understand visibility, reordering and the happens-before rules that make volatile, synchronized and final fields work.

  2. Garbage collection and the heap · Expert · 16 min

    How the JVM reclaims memory: reachability, generations, G1, ZGC and Parallel collectors, heap sizing and memory leaks.

  3. Bytecode and javap · Expert · 15 min

    Read the JVM's stack-based bytecode with javap, and see how method calls, lambdas and string concatenation really compile.

  4. Class loading and initialization · Expert · 14 min

    Follow a class through loading, linking and initialization; understand the loader hierarchy, delegation and NoClassDefFoundError.

  5. JIT compilation and warm-up · Expert · 15 min

    How HotSpot interprets, profiles and compiles hot code with C1 and C2: inlining, escape analysis, deoptimization and warm-up.

  6. Performance tuning and profiling · Expert · 18 min

    Find real bottlenecks with JFR, JDK Mission Control, jcmd and JMH, and fix the usual suspects: allocation, locks, I/O and GC.