Java New Features
March, 2025
Agenda
• Version History and related terms
• Helpful NullPointerException
• Text Blocks
• ZGC: A Scalable Low-Latency Garbage Collector
• Local Variable Type Inference (var)
• Records
• Sealed Classes
• Pattern Matching for switch (Preview)
Version History and related terms
Version History and related terms
• Java Community Process (JCP)
• JDK Enhancement Proposal (JEP)
• Java Specification Request (JSR)
• LTS Support
• Previews Features (95%)
• Experimental Features (25%)
• Enable Preview
Helpful NullPointerException
• Introduced in Java 14
• Helpful during method chaining
• Disabled by default (-XX:+ShowCodeDetailsInExceptionMessages)
• Enabled in Java 15
Text Blocks
• Introduced in Java 15
• Helpful for String formatting (quotes / escape / new line / concat)
ZGC: A Scalable Low-Latency Garbage Collector
• Memory Pools
• Young generation (Eden / survivor 1 / survivor 1)
• Old generation
• Garbage Collection
• Mark / Copy / Remove Phases
• Stop-the-world (STW)
• Major and Minor GC
ZGC: A Scalable Low-Latency Garbage Collector
Choosing the Best Garbage Collection Algorithm
• Throughput - No of transactions
• Latency - time to complete
• Footprint - overhead (memory / resources)
ZGC: A Scalable Low-Latency Garbage Collector
• Introduced in Java 11 (Experimental Features)
• Disabled by default (XX:+UnlockExperimentalVMOptions and -XX:+UseZGC)
• Full concurrent collection along with Java app running
• STW pauses are very short
• Supports large heap size
Local Variable Type Inference (var)
• Introduction
• Achieved by var (Introduced in Java 10)
• Standalone type of the initializer on the RHS, and infers that
• Only for local variable (not for instance / static). WHY ?
• Still a statically type
• variable assigned using var must be initialized with a non-null value.
Local Variable Type Inference (var)
• var is not allowed for –
• Any method parameter
• constructor's parameters
• field or instance variables
• return type for methods
• To hold lambda expression
• as generic / with generic.
• To declare array type
Sealed Classes
• Introduction
• Introduced in Java 17 (2 previews in Java 15 and Java 16)
• More fine-grained inheritance control (security of libraries)
• Introduces 2 new keyword - sealed and non-sealed
• Both class and Interface can be defined as sealed
• sealed class permits classes, and sealed interface permits list of interfaces / classes
• Rules to consider
• Sealed class must have sub-class(es)
• All classes listed in the permits clause must directly extend the sealed class.
• Any other class (not mentioned in permit clause) can't extend the sealed class.
• All direct subclasses of a sealed class must define its accessibility and can only be
sealed / non-sealed / final.
Sealed Classes
Java Records
• Introduced in Java 16 (first preview in Java 14, second preview in Java 15)
• New kind of class, transparent carriers for immutable data
• Provide a clear, concise way to define immutable data structures
• Reduce boilerplate code, improve the reliability
• Most impactful and welcome changes to the Java
Java Records
• A record automatically provides implementations for -
• A concise canonical constructor
• Accessor methods for all fields
• equals() and hashCode() and toString() methods
Java Records
• Quick Facts
• Every property in a record has an implicit getter method
• All properties in a record are final (no need for setters)
• After compilation, <RecordName>.class file is generated
• Records can include static variables and methods
• Records can't extend another class but can implement an interface
• Records can't be abstract as they are final by definition
• We can create immutable classes using Java Records
THANK YOU!
Q&A