Tuesday, June 17, 2025

Java's major new version features

Java's major new version features 


✅ Java 8 (2014) — "Functional Java Begins"

๐Ÿ”น Lambda Expressions

List<String> names = List.of("Sam", "John");
names.forEach(name -> System.out.println("Hi " + name));

Why: Shorter anonymous functions
Real-life: Like sticky notes you don't name.


๐Ÿ”น Streams API

List<String> filtered = names.stream()
                             .filter(name -> name.startsWith("S"))
                             .collect(Collectors.toList());

Why: Clean way to filter, map, reduce
Real-life: Conveyor belt with filters.


๐Ÿ”น Default Methods in Interfaces

interface Animal {
    default void walk() { System.out.println("Walking..."); }
}

Why: Add features without breaking old code


✅ Java 9 (2017) — "Modular Java"

๐Ÿ”น Modules

module com.myapp {
    requires java.base;
}

Why: Better packaging, faster startup
Real-life: Like having labeled suitcases in a large trip.


๐Ÿ”น JShell (REPL)

jshell> int x = 5;
jshell> System.out.println(x * 2);

Why: Quick experiments
Real-life: Like testing a spice while cooking.


✅ Java 10 (2018) — "Type Inference"

๐Ÿ”น var keyword

var list = new ArrayList<String>();

Why: Less boilerplate
Real-life: Like saying “that guy” instead of their full name when context is obvious.


✅ Java 11 (2018) — "Polishing Java 8–10"

๐Ÿ”น var in Lambda Parameters

list.forEach((var name) -> System.out.println(name));

๐Ÿ”น New HTTP Client API

HttpClient client = HttpClient.newHttpClient();

Why: Modern, async-friendly HTTP


๐Ÿ”น Run Java Files without Compilation

java Hello.java

Why: Script-like convenience


✅ Java 12–14 — "Small but Useful"

๐Ÿ”น Switch Expressions (Preview in 12, Stable in 14)

String result = switch(day) {
    case MONDAY -> "Start";
    case FRIDAY -> "Party";
    default -> "Meh";
};

Why: Cleaner switch statements
Real-life: Like a fast menu selection.


✅ Java 15 — "Text Blocks"

๐Ÿ”น Multiline Strings

String json = """
              {
                "name": "ChatGPT",
                "type": "AI"
              }
              """;

Why: Write clean JSON, XML, SQL
Real-life: Copy-pasting real code blocks directly.


✅ Java 16–17 — "Records & Pattern Matching"

๐Ÿ”น record Classes

record User(String name, int age) {}

Why: Auto-generates equals(), hashCode(), etc.
Real-life: Like filling a short form that becomes a full passport.


๐Ÿ”น Pattern Matching for instanceof

if (obj instanceof String s) {
    System.out.println(s.length());
}

✅ Java 18 — "UTF-8 & Simpler APIs"

๐Ÿ”น Default Charset is UTF-8

Why: Consistency across systems


✅ Java 19–20 — "Preview of the Future"

๐Ÿ”น Virtual Threads (Preview)

Thread.startVirtualThread(() -> {
    System.out.println("Hello from virtual thread!");
});

Why: Lightweight threads for massive concurrency
Real-life: Like spawning 1,000 chat bots instead of 1,000 humans.


✅ Java 21 (LTS – 2023) — "Power Features Go Mainstream"

๐Ÿ”น Virtual Threads (Stable)

try (var executor = Executors.newVirtualThreadPerTaskExecutor()) {
    executor.submit(() -> System.out.println("Hello virtual!"));
}

๐Ÿ”น Record Patterns

record Point(int x, int y) {}
Object obj = new Point(1, 2);

if (obj instanceof Point(int x, int y)) {
    System.out.println(x + y);
}

๐Ÿ”น Pattern Matching in switch

switch (obj) {
    case String s -> System.out.println("String: " + s);
    case Integer i -> System.out.println("Int: " + i);
    default -> System.out.println("Unknown");
}

๐Ÿ”š Summary Table

Version Key Features Real-Life Analogy
Java 8 Lambdas, Streams, Default methods Express lanes and personal assistants
Java 9 Modules, JShell Organized luggage, quick tests
Java 10 var Letting the system guess simple types
Java 11 HTTP client, Java file execution No compile step, faster HTTP
Java 14 Switch expressions Modern fast-food style selection
Java 15 Text blocks Pasting whole paragraphs cleanly
Java 17 Records, Pattern Matching Shortcuts for boilerplate data classes
Java 21 Virtual threads, Record patterns Billions of efficient AI threads


No comments:

Post a Comment

LeetCode C++ Cheat Sheet June

๐ŸŽฏ Core Patterns & Representative Questions 1. Arrays & Hashing Two Sum – hash map → O(n) Contains Duplicate , Product of A...