Why Java 14’s New Switch Expression and Null‑Pointer Messages Matter
This article explains Java 14’s revamped switch expression syntax—including the concise case‑label arrow, the new yield keyword for returning values, and improved NullPointerException messages—showing how these changes simplify code and make debugging clearer.
Java 13 was released in September 2019 with almost no useful features, so we skip it and go straight to Java 14 .
Switch expression
The traditional switch statement is verbose and often hides bugs because of the required break statements. An example of the old style is shown below.
switch (day) {
case MONDAY:
case FRIDAY:
case SUNDAY:
System.out.println(6);
break;
case TUESDAY:
System.out.println(7);
break;
case THURSDAY:
case SATURDAY:
System.out.println(8);
break;
case WEDNESDAY:
System.out.println(9);
break;
default:
System.out.println(11);
}Because of its noise, the author prefers to avoid the classic switch expression.
New switch label
Java 14 introduces the case L -> label, allowing multiple constants separated by commas and eliminating the need for break. The same logic can be written as:
switch (day) {
case MONDAY, FRIDAY, SUNDAY -> System.out.println(6);
case TUESDAY -> System.out.println(7);
case THURSDAY, SATURDAY -> System.out.println(8);
case WEDNESDAY -> System.out.println(9);
default -> System.out.println(11);
}The right‑hand side of a case L -> label may be an expression, a block, or a throw statement. To return a value from a switch, you can write:
int numLetters = switch (day) {
case MONDAY, FRIDAY, SUNDAY -> 6;
case TUESDAY -> 7;
case THURSDAY, SATURDAY -> 8;
case WEDNESDAY -> 9;
default -> 11;
};When a switch expression is assigned to a variable, the default branch is mandatory.
Yield restricted identifier
Inside a case that produces a value, the correct way to return the value is with the new yield keyword, not return. Example:
int numLetters = switch (day) {
case WEDNESDAY -> {
System.out.println("day = " + day);
// yield is correct here
yield 9;
};
default -> 11;
};Improved Null‑Pointer messages
Before Java 14, a NullPointerException only reported the line number. Java 14 adds a more descriptive message that shows which variable was null, e.g.:
Exception in thread "main" java.lang.NullPointerException: Cannot store to int array because "arr" is null
at cn.felord.SomeClass.main(SomeClass.java:17)This makes it easier to pinpoint the source of the null pointer.
Other changes
Some preview features were not promoted and may be removed; readers can consult the official Java 14 release notes for details.
References
Java 14 release notes
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.
Programmer DD
A tinkering programmer and author of "Spring Cloud Microservices in Action"
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.
