Nearby lessons

119 of 125

Modern Java — Java 15 to Java 24

📌 What You Will Learn
  • What happened in Java after version 14
  • The new LTS versions: 17 and 21
  • Records and sealed classes — modern class design
  • Virtual threads — the future of concurrency
  • A practical roadmap to stay current

Modern Java — Java 15 to Java 24 is a core concept of the Java language. This lesson explains Why This Chapter Exists, The Versions at a Glance and Java 16 — Stream.toList() and Finalized Features with complete, runnable code examples, clear step-by-step explanations, and common mistakes to avoid with exam-style MCQs at the end.

Why This Chapter Exists

The classic material ends at Java 14. As your trainer with 20+ years of experience, I want you to be employable today — so here is a clear, honest update of everything important that came after Java 14.

The good news: everything you learned in Chapters 1-31 is still 100% valid. Modern Java builds on those foundations. This chapter only adds the new vocabulary on top.

The Versions at a Glance

VersionDateHeadline features
Java 15Sep 2020Text blocks FINAL, sealed classes (preview), ZGC final, helpful NPE default
Java 16Mar 2021Records FINAL, pattern matching for instanceof FINAL, Stream.toList()
Java 17 (LTS)Sep 2021Sealed classes FINAL, pattern matching for switch (preview), long-term support
Java 18-202022Small versions: simple web server, virtual threads (preview), record patterns (preview)
Java 21 (LTS)Sep 2023Virtual threads FINAL, switch patterns FINAL, record patterns FINAL, sequenced collections
Java 22-242024-2025String templates (preview), structured concurrency (preview), more polish
Trainer's Note: The LTS rule: companies run LTS versions — 8, 11, 17, 21. If you are starting a new project in 2026, Java 21 is the safe, modern choice. Everything you learn in this material applies to it directly.

Java 16 — Stream.toList() and Finalized Features

A tiny but beloved addition: Stream.toList() gives a list directly, no Collector needed.

Example03
JCode Cell
1var filtered = numbers.stream()
2 .filter(n -> n > 10)
3 .toList(); // Java 16 - simpler than collect(Collectors.toList())

Java 21 — Virtual Threads (The Big News)

This is the most important change for server developers since Java 8. Traditional threads are heavy — a server can handle only a few thousand. Virtual threads are super-light: one server can handle millions of them.

For a beginner: think of virtual threads as cheap, disposable workers. You can create thousands without worrying about memory. This makes writing high-performance servers much easier.

In simple words: Virtual threads are tiny, cheap threads that you can create by the thousand. The operating system does not manage each one, so a single server can handle millions of tasks instead of just a few thousand.
Trainer's Note: Virtual threads do not replace the need to learn the classic concepts in Chapter 11 (synchronized, wait/notify, thread life cycle). The old ideas are still the base — virtual threads just make creating many threads cheap.
Example04
JCode Cell
1// Old style - one platform thread per task (heavy)
2// new Thread(() -> doWork()).start();
3 
4// Modern - virtual threads (lightweight, Java 21)
5Thread.startVirtualThread(() -> doWork());
6 
7// or through an executor
8var executor = Executors.newVirtualThreadPerTaskExecutor();
9executor.submit(() -> doWork());

Java 21 — Sequenced Collections

A new interface family makes the first and last elements of any collection easy to reach:

In simple words: Sequenced collections give every collection a first and a last element. With getFirst() and getLast() you no longer need indexes or iterators just to touch the two ends.
Example05
JCode Cell
1var list = List.of("A", "B", "C");
2System.out.println(list.getFirst()); // A
3System.out.println(list.getLast()); // C
4 
5var set = new LinkedHashSet<>(List.of(1, 2, 3));
6System.out.println(set.getFirst()); // 1
7System.out.println(set.getLast()); // 3

What's Cooking (Previews) in 2024-2026

  • String templates — safe, readable way to build strings: "Hello \{name}" (preview).
  • Structured concurrency — keep related virtual threads together and handle them as one unit (preview).
  • Scoped values — a clean alternative to thread-local variables (preview/incubating).
  • Foreign Function & Memory API — call native C code safely (preview).

These are advanced. As a student, focus on the final features — text blocks, records, sealed classes, switch patterns, and virtual threads. Previews are for the future.

Your Roadmap to Stay Current

  • Master Java 8 first — lambdas, streams, Optional (Chapter 25). This is the daily language of every job.
  • Learn records and sealed classes — modern class design for clean code.
  • Practice switch patterns and pattern matching — they replace lots of old if-else code.
  • Understand virtual threads conceptually — they will be in your future interviews.
  • Build a small Spring Boot + MySQL project — it ties JDBC, collections, and modern Java together.
  • Keep an eye on the official Java version pages every release — a 10-minute read every six months keeps you never-outdated.
Trainer's Note: Final words from your trainer: Java's power is that it never throws away its past. The Java you learned in this material will be useful in every project for the rest of your career. Study honestly, type every example by hand, break programs on purpose to learn from errors, and you will be an excellent Java developer. All the best!
📝 Key Takeaways
  • Modern Java is released every 6 months; LTS versions are 8, 11, 17, 21.
  • Text blocks and records make code shorter and cleaner.
  • Sealed classes control exactly who can extend a class.
  • Pattern matching for switch works on types and null.
  • Virtual threads (Java 21) let servers handle millions of tasks.
  • Previews (templates, structured concurrency) are the future — learn the finalized features first.
  • Java 8 concepts remain the everyday foundation in every job.

🧠 Test Your Knowledge

4 Questions
Progress: 0 / 4