Java 13 from Java 9 to Java 17

Java 13 was released in September 2019. There are few officially available features introduced in this version, which is one of the most boring versions in my opinion. So let's skip Java 13 and look directly at Java 14.

Switch expression

I think the switch expression is rarely used in daily development, not because the keyword is too "difficult" compared with if, but because it is very lengthy, and this visual noise is easy to cover up the errors that are difficult to debug, especially the break statements, as follows:

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);    
}

It's not clear at all, so I usually refuse to use switch expressions when writing Java.

New switch tag

In order to simplify the switch, Java introduces the switch tag case L - > feature. If the input matches L, only the code on the right side of the tag will be executed. We also recommend that each case allow multiple constants separated by commas. The previous code can now 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 code to the right of the case L - > Switch tag is restricted to expressions, code blocks, or throw statements. If we want the Switch statement to have a return value, it can be written as follows:

int numLetters = switch (day) {
    case MONDAY, FRIDAY, SUNDAY -> 6;
    case TUESDAY                -> 7;
    case THURSDAY, SATURDAY     -> 8;
    case WEDNESDAY              -> 9;
    default                     -> 11;    
};

❝ the default branch is required when the Switch statement is assigned to a variable.

yield restricted identifier

Case L - > when there is a code block on the right and a value will be generated according to the case condition, it will be written as follows according to our thinking inertia:

        int numLetters = switch (day) {
      
            case WEDNESDAY              -> {
                System.out.println("day = " + day);
                // Incorrect writing will cause compilation errors
                return 9;
            };
            default -> 11;
        };

You will find that the above writing method can not pass the compilation. This is a context problem. When the case condition is met, it does not mean to return a value, but to yield a value. Therefore, a new restricted identifier yield (note that it is not a keyword) is introduced to deal with this context problem.

        int numLetters = switch (day) {
      
            case WEDNESDAY              -> {
                System.out.println("day = " + day);
                // yield is right
                yield 9;
            };
            default -> 11;
        };

More reasonable null pointer hint

Null pointer exception is one of the most common exceptions in Java. In the null pointer exception stack information before Java 14, it only indicates that a NullPointerException has occurred in a given line, which is not intuitive and clear enough:

Exception in thread "main" java.lang.NullPointerException
at cn.felord.SomeClass.main(SomeClass.java:17)

Now the log will explain the specific source of the null pointer:

Exception in thread "main" java.lang.NullPointerException: Cannot store to int array because "arr" is null
at cn.felord.SomeClass.main(SomeClass.java:17)

We can clearly know that arr is null, resulting in a null pointer.

Other changes

Since some preview features are not confirmed and may even be removed in the future, they will not be described in detail here. If you are interested, you can view it on the Java 14 release log [1]. The follow-up will bring you more valuable content output.

reference material

[1]Java 14 release log: https://openjdk.java.net/projects/jdk/14/

Added by s3rg1o on Thu, 09 Dec 2021 02:33:14 +0200