Java 12 Features

Author: Shazin Sadakath


We talked about Java 10 and 11 in a previous article. Today we are going to see the upcoming version of Java which is version 12 and what features, enhancements we can expect in it.

The JDK 12 Feature set is frozen now which means it is high time to discuss about features which will be released in early February, with the GA release in mid March. Out of many features following are the most awaited and noteworthy.

Removal of raw String literals

A raw String literal is described as a String literal which can span multiple lines of source code and does not interpret escape sequences, such as \n, or Unicode escapes of the form \uXXXX. So following can code expression:

String javascript = "function test(name) {\n" +
                    "   console.log(name);\n" +
                    "}\n";

Can now be written as following:

String javascript = `function test(name)
                        console.log(name);
                     }`;

Which is much more easier way to define multiple lines of source code.

An enhanced switch statement

With JEP 325: Switch Expression now the operator -> can be used to instruct that only that particular piece of code needs to be executed. This eliminates the need to use break keyword to stop the execution flow after a particular case is met in a switch statement. So following switch code:

switch(no) {
    case 10 -> System.out.println("Ten");
    case 8 -> System.out.println("8");
    default -> System.out.println("Unknown Number");
}

Also the switch statement is modified to return a value so that it can be used in an RHS side of an assignment expresion like below.

String result = switch (arg) {
    case 1 -> "One";
    case 10 -> "Ten";
    default -> "Unknown";
};

This is a handy change which eliminates a lot of boilerplate code in the source code file and will make code much more readable.

Other features in JDK 12 will include:

 



Tags: Java 12 JDK12 Features
Views: 585
Register for more exciting articles

Comments

Please login or register to post a comment.


There are currently no comments.