Java 14 - ashishranjandev/interview-wiki GitHub Wiki

☕ Java 14: The Complete Guide

Release Date: March 2020 Type: Non-LTS (Feature Release) Focus: Reducing verbosity (Records, Pattern Matching) and improving debugging (Helpful NPEs).


1. Records (Preview) 📼

JEP 359 This was the headline feature. It aimed to eliminate the noisy "POJO" (Plain Old Java Object) boilerplate that developers had been writing for 25 years.

Comparison

Pre-Java 14 (The "Verbose" Way) To create a simple data carrier for a Point (x, y), you had to write lines of getters, toString, equals, and hashCode methods.

public class Point {
    private final int x;
    private final int y;

    public Point(int x, int y) {
        this.x = x;
        this.y = y;
    }

    // Getters...
    // equals()...
    // hashCode()...
    // toString()... 
    // (Over 50 lines of code just to hold two integers!)
}

Java 14 (The Record Way) The compiler automatically generates the constructor, accessors, equals, hashCode, and toString from the header.

public record Point(int x, int y) {} 
// (1 line of code)

Concept: Data as Data

Records imply that the data is immutable and transparent.

classDiagram
    class ClassPoint {
        -int x
        -int y
        +getX()
        +getY()
        +equals()
        +hashCode()
        +toString()
    }
    class RecordPoint {
        <<record>>
        int x
        int y
    }
    note "Records strip away the 'behavioral' boilerplate<br/>to focus purely on the data."

Loading

2. Helpful NullPointerExceptions 🕵️‍♂️

JEP 358 A massive quality-of-life improvement for debugging.

Comparison

Pre-Java 14 (The "Guessing Game") If you had a chain of calls and one failed, the JVM only told you the line number.

a.getB().getC().send(); 
// Exception in thread "main" java.lang.NullPointerException
//      at Main.main(Main.java:15)
  • Problem: Was a null? Was getB() null? Was getC() null? You didn't know without adding print statements or using a debugger.

Java 14 (The "Helpful" Way) The JVM precisely identifies which variable was null.

// Exception in thread "main" java.lang.NullPointerException: 
// Cannot invoke "C.send()" because the return value of "B.getC()" is null

3. Pattern Matching for instanceof (Preview) 🧩

JEP 305 Simplifying the common idiom of "check type, then cast type."

Comparison

Pre-Java 14 (The "Check-Then-Cast" Dance) You had to repeat the class name three times: once to check, once to cast, and once to declare.

if (obj instanceof String) {
    String s = (String) obj; // Manual cast required
    if (s.length() > 5) {
        System.out.println(s.toUpperCase());
    }
}

Java 14 (The "Smart" Way) If the check succeeds, the variable is automatically cast and assigned to a new variable name.

if (obj instanceof String s) { // Check + Cast + Declare
    if (s.length() > 5) {
        System.out.println(s.toUpperCase());
    }
}
flowchart LR
    Start(Object obj) --> Check{Is obj a String?}
    Check -- No --> End
    Check -- Yes --> Bind[Auto-bind to variable 's']
    Bind --> Use[Use 's' as a String directly]

Loading

4. Under the Hood: ZGC on macOS & Windows 🌍

JEP 364 & 365 Expanding the reach of the Z Garbage Collector.

  • Pre-Java 14: ZGC (the ultra-low latency collector) was only available on Linux.
  • Java 14: Ported ZGC to macOS and Windows, allowing developers on these platforms to test and use low-latency GC strategies.

5. Under the Hood: Removal of CMS GC 🗑️

JEP 363 Cleaning up technical debt.

  • The Change: The Concurrent Mark Sweep (CMS) garbage collector was deprecated in Java 9. In Java 14, it was completely removed.
  • Rationale: The maintenance burden was high, and G1 (the default) and ZGC had surpassed it in performance and reliability.
⚠️ **GitHub.com Fallback** ⚠️