JDK 15 features
Table of Contents
- π What is JDK 15
- π Why JDK 15 is important
- π Key features introduced in JDK 15
- π Internal working of major features
- π Implementation in Java
- π Summary
- π Interview Questions
π What is JDK 15
JDK 15 is a non-LTS (Short-Term Release) version of Java, but donβt underestimate it. It introduced several important features that later became stable in JDK 17.
Think of JDK 15 as an innovation release β where new ideas were introduced, tested, and refined.
π Many features like Text Blocks and Sealed Classes started gaining real traction here.
π Why JDK 15 is important
Even though it’s not LTS, JDK 15 plays a critical role in Java evolution.
π₯ Why it matters:
- β Introduces preview features (future of Java)
- β Improves developer productivity
- β Enhances JVM internals
- β Reduces boilerplate code
- β Prepares foundation for JDK 17
π If you understand JDK 15, you understand the direction Java is heading.
π Key JDK 15 Features
Letβs break down the most important features in a practical way.
π₯ 1. Text Blocks (Standard Feature)
Writing multi-line strings was always messy in Java.
JDK 15 finally fixes that.
β Internal Working Insight
- Uses triple quotes “””
- Preserves formatting
- Removes unnecessary escape characters
- Compiler normalizes indentation
π Cleaner code, especially for JSON, SQL, XML.
π» Implementation in Java
public class TextBlockExample {
public static void main(String[] args) {
String json = """
{
"name": "Java",
"version": 15
}
""";
System.out.println(json);
}
}
π No more \n and ” everywhere.
π₯ 2. Sealed Classes (Preview)
Sealed classes restrict which classes can extend them.
β Internal Working Insight
- Uses
permitskeyword - JVM verifies allowed subclasses
- Helps in domain modeling
π Makes inheritance controlled and predictable.
π» Implementation in Java
sealed class Shape permits Circle, Rectangle {
}
final class Circle extends Shape {
}
final class Rectangle extends Shape {
}
π Prevents unwanted extensions.
π₯ 3. Hidden Classes
This is a JVM-level feature, mainly useful for frameworks.
β Internal Working Insight
- Classes are not discoverable via reflection
- Used internally by frameworks like proxies
- Short-lived and dynamically generated
π Improves performance and security for dynamic class generation.
π» Example Concept
// No direct usage like normal classes
// Used internally via MethodHandles.Lookup
π You wonβt use it directly, but frameworks do.
π₯ 4. ZGC Improvements
Z Garbage Collector continues to evolve.
β Internal Working Insight
- Reduced latency
- Improved memory handling
- Better scalability
π Designed for large-scale, low-latency systems.
π₯ 5. Shenandoah Garbage Collector (Production Ready)
Another low-pause GC option.
β Internal Working Insight
- Performs concurrent compaction
- Reduces pause times significantly
- Ideal for real-time applications
π Now production-ready in JDK 15.
π₯ 6. Records (Second Preview)
Records were refined further in JDK 15.
β Internal Working Insight
- Immutable data carriers
- Compiler auto-generates boilerplate code
π Simplifies DTO creation.
π» Implementation in Java
record User(String name, int age) {
}
public class RecordExample {
public static void main(String[] args) {
User user = new User("Alice", 25);
System.out.println(user.name());
}
}
π No getters/setters needed.
π₯ 7. Pattern Matching for instanceof (Second Preview)
Further refinement of pattern matching.
β Internal Working Insight
- Combines type check and casting
- Reduces runtime errors
π» Implementation in Java
public class PatternMatchingExample {
public static void main(String[] args) {
Object obj = "Java";
if (obj instanceof String str) {
System.out.println(str.length());
}
}
}
π Cleaner and safer code.
π₯ 8. EdDSA Cryptographic Algorithm
New cryptographic signature algorithm added.
β Internal Working Insight
- Faster and more secure than traditional algorithms
- Uses Edwards-curve Digital Signature Algorithm
π Important for security-focused applications.
π₯ 9. Foreign-Memory Access API (Incubator)
Allows Java to access memory outside JVM heap.
β Internal Working Insight
- Works without JNI
- Safer and more efficient
- Uses MemorySegment abstraction
π Useful for high-performance applications.
π» Conceptual Example
// Simplified concept (API was incubator)
π This is the future of native memory access in Java.
π Implementation in Java
Letβs combine multiple JDK 15 features in one example π
sealed interface Vehicle permits Car, Bike {
}
record Car(String name, int speed) implements Vehicle {
}
record Bike(String name, int speed) implements Vehicle {
}
public class JDK15Demo {
public static void printDetails(Vehicle vehicle) {
if (vehicle instanceof Car c) {
System.out.println("Car: " + c.name() + ", Speed: " + c.speed());
} else if (vehicle instanceof Bike b) {
System.out.println("Bike: " + b.name() + ", Speed: " + b.speed());
}
}
public static void main(String[] args) {
String text = """
Welcome to JDK 15
Features Demo
""";
System.out.println(text);
Vehicle v1 = new Car("Tesla", 200);
Vehicle v2 = new Bike("Yamaha", 120);
printDetails(v1);
printDetails(v2);
}
}
π Notice how modern and clean Java starts to feel.
π Summary
JDK 15 is a feature-rich release that pushed Java toward modernization.
π₯ Key takeaways:
- Text Blocks simplify multi-line strings
- Sealed Classes introduce controlled inheritance
- Records reduce boilerplate
- Pattern Matching improves readability
- JVM improvements enhance performance
π JDK 15 laid the groundwork for JDK 17.
If you want to understand deeper Java internals, check this:
https://code2java.com/internal-implementation-of-hashmap/
π Interview Questions
π 1. What are the major features introduced in JDK 15?
π 2. What are Text Blocks and why are they useful?
π 3. Explain Sealed Classes with examples.
π 4. What are Hidden Classes in JVM?
π 5. How do Records reduce boilerplate code?
π 6. What is Pattern Matching for instanceof?
π 7. What improvements were made in garbage collectors?
π 8. What is the Foreign-Memory Access API?
