A comprehensive guide to all Java 15 concepts with practical examples for interview preparation.
- Sealed Classes (Preview)
- Hidden Classes
- Text Blocks (Finalized)
- Records (Second Preview)
- Pattern Matching for instanceof (Second Preview)
- Foreign-Memory Access API (Second Incubator)
- Edwards-Curve Digital Signature Algorithm
- ZGC Production Ready
- Shenandoah GC Production Ready
- Removed and Deprecated Features
- Common Interview Questions
Classes that restrict which classes can extend them.
// Sealed class - only permits specific classes to extend
public sealed class Shape
permits Circle, Rectangle, Triangle {
// Class definition
}
// Permitted subclasses
public final class Circle extends Shape {
private final double radius;
public Circle(double radius) {
this.radius = radius;
}
}
public final class Rectangle extends Shape {
private final double width, height;
public Rectangle(double width, double height) {
this.width = width;
this.height = height;
}
}
public final class Triangle extends Shape {
private final double base, height;
public Triangle(double base, double height) {
this.base = base;
this.height = height;
}
}
// Other classes cannot extend Shape
// public class Polygon extends Shape {} // Compile errorpublic sealed interface Vehicle
permits Car, Truck, Motorcycle {
void start();
}
public final class Car implements Vehicle {
@Override
public void start() {
System.out.println("Car starting");
}
}
public final class Truck implements Vehicle {
@Override
public void start() {
System.out.println("Truck starting");
}
}
public final class Motorcycle implements Vehicle {
@Override
public void start() {
System.out.println("Motorcycle starting");
}
}final: Cannot be extendedsealed: Can be further restrictednon-sealed: Can be extended by anyone
public sealed class Animal
permits Dog, Cat, Bird {
}
public final class Dog extends Animal {} // Final - cannot extend
public sealed class Cat extends Animal
permits Persian, Siamese { // Sealed - further restricted
}
public non-sealed class Bird extends Animal {} // Non-sealed - can extend freely
public class Parrot extends Bird {} // OK - Bird is non-sealed- Exhaustive pattern matching (switch knows all possible types)
- Better API design (restricted inheritance)
- Improved type safety
- Document intended hierarchy
2. Hidden Classes
Classes that cannot be used directly by other classes' bytecode.
Hidden classes are classes that cannot be used directly by the bytecode of other classes. They are intended for use by frameworks that generate classes at runtime and use them indirectly through reflection.
- Cannot be linked by other classes
- Not discoverable by reflection (unless explicitly allowed)
- Can be unloaded independently
- Created at runtime
- Not part of the package hierarchy
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodHandles.Lookup;
// Hidden classes are typically created by frameworks
MethodHandles.Lookup lookup = MethodHandles.lookup();
Class<?> hiddenClass = lookup.defineHiddenClass(byteCode, true).lookupClass();
// Hidden class can be used via reflection or method handles
// But not directly referenced in bytecode- Frameworks loading classes dynamically
- Language runtimes (e.g., lambdas, proxies)
- Application servers
- Dynamic code generation
- Performance optimization (faster class loading)
- Better encapsulation
- Can be unloaded when not needed
- Faster class loading
- Framework-friendly API
Note: For detailed documentation and examples, see Hidden Classes Guide.
Text blocks are now a standard feature (no longer preview).
Text blocks provide multi-line string literals without the need for escape sequences, improving code readability.
// Finalized - no longer preview, no --enable-preview needed
String html = """
<html>
<body>
<p>Hello, World!</p>
</body>
</html>
""";
// JSON example
String json = """
{
"name": "John",
"age": 30,
"city": "New York"
}
""";
// SQL example
String sql = """
SELECT id, name, email
FROM users
WHERE status = 'active'
ORDER BY name
""";// Quotes don't need escaping (except triple quotes)
String text = """
She said "Hello"
""";
// Escape newline (suppress line break)
String text2 = """
Line 1 \
Line 2
"""; // Results in "Line 1 Line 2"
// Standard escape sequences work
String text3 = """
Tab:\tTab
Newline:\nNewline
""";- Better readability for multi-line strings
- Preserves formatting
- Easier to write HTML, JSON, SQL, etc.
- Reduces escape sequences needed
- Standard feature (no preview flag)
Refinements to records feature based on feedback.
Records provide a compact syntax for declaring classes that are transparent holders for shallowly immutable data.
// Simple record
public record Point(int x, int y) {}
// Usage
Point point = new Point(3, 4);
System.out.println(point.x()); // 3
System.out.println(point.y()); // 4
System.out.println(point); // Point[x=3, y=4]public record Person(String name, int age) {
// Compact constructor - validates before assignment
public Person {
if (age < 0) {
throw new IllegalArgumentException("Age cannot be negative");
}
if (name == null || name.isBlank()) {
throw new IllegalArgumentException("Name cannot be null or blank");
}
}
// Additional methods
public boolean isAdult() {
return age >= 18;
}
// Static methods
public static Person of(String name, int age) {
return new Person(name, age);
}
}- Immutable by default
- All fields are final
- Automatic equals(), hashCode(), toString()
- Accessor methods (fieldName(), not getFieldName())
- Can have constructors, methods, static methods
- Cannot extend other classes (implicitly extends Record)
- Can implement interfaces
Refinements to pattern matching based on feedback.
Pattern matching for instanceof simplifies the common pattern of checking an object's type and casting it.
Object obj = "Hello";
// Pattern matching - automatic casting
if (obj instanceof String str) {
// str is automatically cast and available
System.out.println(str.toUpperCase());
System.out.println(str.length());
}Object value = "Hello World";
// Combine pattern matching with conditions
if (value instanceof String str && str.length() > 5) {
System.out.println("Long string: " + str);
}Object obj = "Hello";
if (!(obj instanceof String str)) {
System.out.println("Not a string");
} else {
// str is available in else block
System.out.println(str.length());
}- Eliminates manual casting
- Reduces boilerplate
- Prevents cast errors
- Improves readability
- Type-safe operations
API for safely accessing memory outside the Java heap.
The Foreign-Memory Access API (second incubator) provides a way to access native memory safely and efficiently, useful for interop with native libraries.
import jdk.incubator.foreign.*;
// Allocate native memory
try (var scope = MemorySession.openConfined()) {
MemorySegment segment = MemorySegment.allocateNative(1024, scope);
// Use the memory segment
MemoryAccess.setIntAtOffset(segment, 0, 42);
int value = MemoryAccess.getIntAtOffset(segment, 0);
System.out.println(value); // 42
}
// Memory automatically freed when scope is closed- Safe memory access (bounds checking)
- Automatic resource management
- Type-safe operations
- Useful for native library interop
- Interfacing with native libraries
- High-performance memory operations
- System-level programming
- Zero-copy operations
Note: This is an incubator feature and requires --add-modules jdk.incubator.foreign and --enable-preview flags.
New cryptographic algorithm support for EdDSA (Edwards-Curve Digital Signature Algorithm).
Java 15 adds support for the Edwards-Curve Digital Signature Algorithm (EdDSA), providing better security and performance for digital signatures.
- Ed25519: 255-bit curve
- Ed448: 448-bit curve
import java.security.*;
import java.security.spec.*;
import java.util.Base64;
// Generate EdDSA key pair
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("Ed25519");
KeyPair keyPair = keyGen.generateKeyPair();
// Sign data
Signature signature = Signature.getInstance("Ed25519");
signature.initSign(keyPair.getPrivate());
byte[] data = "Hello, Java 15!".getBytes();
signature.update(data);
byte[] signatureBytes = signature.sign();
// Verify signature
signature.initVerify(keyPair.getPublic());
signature.update(data);
boolean verified = signature.verify(signatureBytes);
System.out.println("Signature verified: " + verified);- Better security
- Improved performance
- Smaller signature sizes
- Modern cryptographic algorithm
- Digital signatures
- Authentication
- Secure communications
- Certificate generation
Z Garbage Collector is now production-ready (no longer experimental).
ZGC (Z Garbage Collector) is a scalable, low-latency garbage collector designed for applications that require low pause times and large heap sizes.
# Use Z GC (production ready - no experimental flag needed)
java -XX:+UseZGC MainClass
# With additional options
java -XX:+UseZGC \
-XX:MaxGCPauseMillis=10 \
-Xmx16g \
MainClass- Low Pause Times: Sub-millisecond pause times (< 10ms)
- Large Heaps: Scales to multi-terabyte heaps
- Concurrent: Most operations are concurrent
- Production Ready: No longer experimental
-XX:+UseZGC: Enable Z Garbage Collector-XX:MaxGCPauseMillis=<ms>: Target maximum pause time-XX:ZUncommitDelay=<seconds>: Delay before uncommitting unused memory
- Very low pause times
- Handles large heaps efficiently
- Concurrent operations
- Production-ready stability
- Suitable for latency-sensitive applications
- Low-latency applications
- Large heap applications
- Real-time systems
- High-throughput applications
Shenandoah Garbage Collector is now production-ready (no longer experimental).
Shenandoah is a low-pause-time garbage collector designed for applications that require consistent pause times regardless of heap size.
# Use Shenandoah GC (production ready - no experimental flag needed)
java -XX:+UseShenandoahGC MainClass
# With additional options
java -XX:+UseShenandoahGC \
-XX:MaxGCPauseMillis=10 \
-Xmx16g \
MainClass- Low Pause Times: Consistent pause times (< 10ms) independent of heap size
- Concurrent Evacuation: Moves objects concurrently with application threads
- Large Heaps: Efficiently handles multi-gigabyte heaps
- Production Ready: No longer experimental
-XX:+UseShenandoahGC: Enable Shenandoah Garbage Collector-XX:MaxGCPauseMillis=<ms>: Target maximum pause time-XX:ShenandoahGCHeuristics=<heuristic>: Choose GC heuristic
- Consistent low pause times
- Independent of heap size
- Concurrent evacuation
- Production-ready stability
- Suitable for latency-sensitive applications
- Low-latency applications
- Large heap applications
- Real-time systems
- Applications requiring consistent pause times
| Feature | ZGC | Shenandoah |
|---|---|---|
| Pause Times | < 10ms | < 10ms |
| Heap Size | Multi-terabyte | Multi-gigabyte |
| Concurrent | Yes | Yes |
| Production Ready | Java 15 | Java 15 |
The Nashorn JavaScript Engine has been removed from Java 15.
Impact:
jdk.scripting.nashornmodule no longer availablejjstool removed- JavaScript execution in Java no longer supported
Migration:
- Use GraalVM JavaScript engine
- Use external JavaScript engines (Rhino, etc.)
- Consider alternative scripting solutions
Support for Solaris and SPARC platforms has been removed.
Impact:
- No JDK builds for Solaris/SPARC
- Applications must migrate to other platforms
The Remote Method Invocation (RMI) Activation System has been deprecated for removal in a future release.
Impact:
java.rmi.activationpackage deprecated- RMI activation will be removed in a future Java version
Migration:
- Use RMI without activation
- Consider alternative distributed computing solutions
- Plan migration before removal
When using deprecated features, you'll see warnings like:
warning: [deprecation] RMI Activation is deprecated and will be removed
Recommendation: Update code to remove deprecated features before they are removed.
A: Sealed classes restrict which classes can extend them:
- Exhaustive pattern matching (switch knows all possible types)
- Better API design (restricted inheritance)
- Improved type safety
- Document intended class hierarchy
A: Permitted classes must be:
final: Cannot be extendedsealed: Can be further restrictednon-sealed: Can be extended by anyone
A: The compiler knows all possible subtypes, enabling exhaustive pattern matching in switch expressions without default case (when all types are handled).
Last Updated: 2024
Version: 1.0