Java 14 brought a wave of exciting new features. Pattern Matching for instanceof (preview) offered a more concise way to perform type checks. JFR Event Streaming allowed for real-time analysis of performance data. Records (preview) provided a simpler way to define immutable data objects. NullPointerException messages were enhanced to provide more informative error messages.
March 2020
Java 14 brought a wave of exciting new features. Pattern Matching for instanceof (preview) offered a more concise way to perform type checks. JFR Event Streaming allowed for real-time analysis of performance data. Records (preview) provided a simpler way to define immutable data objects. NullPointerException messages were enhanced to provide more informative error messages.
Eliminate explicit casting after instanceof checks
// Traditional approach
if (obj instanceof String) {
String str = (String) obj;
System.out.println(str.toUpperCase());
}
// Pattern matching for instanceof (Java 14 preview)
if (obj instanceof String str) {
System.out.println(str.toUpperCase());
}
// More complex example
public void processShape(Object shape) {
if (shape instanceof Circle circle) {
System.out.println("Circle with radius: " + circle.radius());
} else if (shape instanceof Rectangle rect) {
System.out.println("Rectangle: " + rect.width() + "x" + rect.height());
} else if (shape instanceof Triangle triangle && triangle.isValid()) {
System.out.println("Valid triangle with area: " + triangle.area());
}
}Concise syntax for immutable data classes
// Traditional data class
public final class Person {
private final String name;
private final int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String name() { return name; }
public int age() { return age; }
@Override
public boolean equals(Object o) { /* ... */ }
@Override
public int hashCode() { /* ... */ }
@Override
public String toString() { /* ... */ }
}
// Record (Java 14 preview)
public record Person(String name, int age) {
// Optional: custom constructor with validation
public Person {
if (age < 0) {
throw new IllegalArgumentException("Age cannot be negative");
}
}
// Optional: additional methods
public boolean isAdult() {
return age >= 18;
}
}
// Usage
Person person = new Person("John Doe", 30);
System.out.println(person.name()); // John Doe
System.out.println(person.age()); // 30
System.out.println(person.isAdult()); // trueJava 14 standardizes switch expressions with arrow syntax and yield statements, providing more concise and expressive conditional logic compared to traditional switch statements.
// Traditional switch statement
String dayType;
switch (dayOfWeek) {
case MONDAY:
case TUESDAY:
case WEDNESDAY:
case THURSDAY:
case FRIDAY:
dayType = "Weekday";
break;
case SATURDAY:
case SUNDAY:
dayType = "Weekend";
break;
default:
dayType = "Unknown";
}
// Switch expression with arrow syntax (Java 14)
String dayType = switch (dayOfWeek) {
case MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY -> "Weekday";
case SATURDAY, SUNDAY -> "Weekend";
default -> "Unknown";
};
// Switch expression with yield for complex logic
int numLetters = switch (dayOfWeek) {
case MONDAY, FRIDAY, SUNDAY -> 6;
case TUESDAY -> 7;
case THURSDAY, SATURDAY -> 8;
case WEDNESDAY -> 9;
default -> {
System.out.println("Unknown day");
yield 0;
}
};