Java 15 focused on both standardization and introducing new features as previews. Several features, including Text Blocks, ZGC, and Shenandoah, graduated to standard status. Sealed Classes (preview) offered more control over inheritance, and Hidden Classes allowed for better encapsulation.
September 2020
Java 15 focused on both standardization and introducing new features as previews. Several features, including Text Blocks, ZGC, and Shenandoah, graduated to standard status. Sealed Classes (preview) offered more control over inheritance, and Hidden Classes allowed for better encapsulation.
Control which classes can extend or implement a type
/*
* What are sealed classes in Java 15?
*
* Sealed classes are a new feature that allows you to control which classes
* can extend or implement a type. They provide a way to define a closed
* hierarchy of classes, ensuring that only permitted classes can be subclasses.
* This enables better domain modeling and improves pattern matching capabilities.
*/
// Define a sealed class hierarchy
public sealed class Shape
permits Circle, Rectangle, Triangle {
public abstract double area();
}
// Permitted subclasses
public final class Circle extends Shape {
private final double radius;
public Circle(double radius) {
this.radius = radius;
}
@Override
public double area() {
return Math.PI * 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;
}
@Override
public double area() {
return width * height;
}
}
// Sealed interface
public sealed interface Vehicle
permits Car, Truck, Motorcycle {
void start();
void stop();
}
// Usage with pattern matching
public void processShape(Shape shape) {
switch (shape) {
case Circle c -> System.out.println("Circle: " + c.area());
case Rectangle r -> System.out.println("Rectangle: " + r.area());
case Triangle t -> System.out.println("Triangle: " + t.area());
// No default needed - compiler knows all possibilities
}
}Java 19 continues development of the Vector API as an incubator feature, providing SIMD (Single Instruction, Multiple Data) operations for high-performance computing. This API enables developers to write vectorized code that can leverage CPU vector instructions, significantly improving performance for mathematical computations, data processing, and scientific applications.
import jdk.incubator.vector.*;
// Vector operations for high-performance computing
public class VectorExample {
static final VectorSpecies<Float> SPECIES = FloatVector.SPECIES_PREFERRED;
public static void vectorAdd(float[] a, float[] b, float[] c) {
int i = 0;
int upperBound = SPECIES.loopBound(a.length);
for (; i < upperBound; i += SPECIES.length()) {
FloatVector va = FloatVector.fromArray(SPECIES, a, i);
FloatVector vb = FloatVector.fromArray(SPECIES, b, i);
FloatVector vc = va.add(vb);
vc.intoArray(c, i);
}
// Handle remaining elements
for (; i < a.length; i++) {
c[i] = a[i] + b[i];
}
}
public static float vectorSum(float[] array) {
FloatVector sum = FloatVector.zero(SPECIES);
int i = 0;
int upperBound = SPECIES.loopBound(array.length);
for (; i < upperBound; i += SPECIES.length()) {
FloatVector va = FloatVector.fromArray(SPECIES, array, i);
sum = sum.add(va);
}
float result = sum.reduceLanes(VectorOperators.ADD);
// Handle remaining elements
for (; i < array.length; i++) {
result += array[i];
}
return result;
}
}