Java 14 Made Easy: Records, Pattern Matching, Switch Expressions & Clear NPEs
This article explains how Java 14 introduces Records to reduce boilerplate class definitions, Pattern Matching to streamline instanceof checks, Switch expressions to treat switch as a value-producing construct, and enhanced NullPointerException messages that pinpoint the exact null source, making code clearer and more concise.
Java 14 adds several language features that simplify common coding patterns and improve error reporting.
1. Record
Traditionally, defining a simple data class requires writing fields, constructors, getters, setters, and overriding toString, equals, and hashCode. The example below shows the verbose version:
public class Student {
private int id;
private String name;
public Student() {
// ...
}
public int getId() {
// ...
}
// ... other getter()/setter()
public boolean equals(Student s) {
// ...
}
public int hashCode() {
// ...
}
public String toString() {
// ...
}
}With record , the same class can be declared in a single line, and the compiler automatically generates the boilerplate methods: record Student(int id, String name) { } Records define a final class with immutable fields, so no setters are generated and field access is direct.
2. Pattern Matching
Traditional instanceof checks require a separate cast:
public void attendTo(Animal a) {
if (a instanceof Dog) {
walk((Dog) a);
} else if (a instanceof Cat) {
cleanLitterBoxOf((Cat) a);
} else {
returnToStore(a);
}
}Pattern matching combines the type test and variable declaration:
public void attendTo(Animal a) {
if (a instanceof Dog d) {
walkDog(d);
} else if (a instanceof Cat c) {
cleanLitterBoxOf(c);
} else {
returnToStore(a);
}
}This eliminates the explicit cast and makes the code clearer.
3. Switch Expressions
Prior to Java 14, switch was a statement. It can now be used as an expression that yields a value:
int i = ...;
String s = switch (i) {
case 0 -> "none";
case 1 -> "one";
default -> "many";
};Another example shows a compact way to compute the number of days in a month:
int days = switch (month) {
case 1, 3, 5, 7, 8, 10, 12 -> 31;
case 4, 6, 9 -> 30;
case 2 -> 28;
default -> 0;
};4. Improved NullPointerException
Traditional NPE messages only indicate the line where the null occurred, leaving developers to guess which method returned null:
Exception in thread "main" java.lang.NullPointerException
at xxx.HelpfulNullPointerException.main(HelpfulNullPointerException.java:10)Java 14 enhances the message to show the exact null source:
Exception in thread "main" java.lang.NullPointerException:
Cannot invoke "String.toLowerCase()" because the return value of
"xxx.HelpfulNullPointerException$PersonalDetails.getEmailAddress()" is null
at xxx.HelpfulNullPointerException.main(HelpfulNullPointerException.java:10)These features collectively reduce boilerplate, make type checks safer, enable more expressive control flow, and provide clearer diagnostics.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Java High-Performance Architecture
Sharing Java development articles and resources, including SSM architecture and the Spring ecosystem (Spring Boot, Spring Cloud, MyBatis, Dubbo, Docker), Zookeeper, Redis, architecture design, microservices, message queues, Git, etc.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
