What is new in Java 12 (for Developers)

25 Mar 2019
4 mins read

Java JDK 12 was released on March 19 as part of the new release cicle (a new Java version every 6 months). In this article we will learn what are the new features available for developers.

What’s new in Java 12?

For Java JDK 12, there are not many new API features. Most enhancements are internal Java/JVM improvements, but there is one new Java feature (JEP) for us, developers.

Below is a list of all features included in Java 12 (from Oracle blog):

Also JEP 326 – Raw String Literals was withdrawn from JDK 12. However, there are 2 new String methods from this JEP that were included in JDK 12.

Let’s take a look at Switch Expressions and the new String methods.

Before you try these new features, don’t forget to download and install the latest JDK. Also, don’t forget to download the latest version of your favorite IDE (Eclipse IDE 2019‑03 or IntelliJ IDEA 2019.1).

New String methods (from JEP 326): indent and transform

There are two new String methods available in JDK 12: indent and transform. Both methods are from JEP 326 – Raw String Literals that was withdrawn from JDK 12.

First method is indent, which as the name says, will apply indentation (number of spaces) as demonstrated below:

var result = "Hello\nWorld!".indent(3);
System.out.println(result);

Output will be:

   Hello
   World!

Second method is transform, which receives a function/lambda with a String as paramater and returns an object.

var result = "Hello".transform(s -> s + ", World!");
System.out.println(result); // Hello, World!

This method can also be chained. This is very useful and easier to read in case we need to transform/manipulate a string more than once:

var result = "hello"
        .transform(s -> s + ", world!")
        .transform(String::toUpperCase);
System.out.println(result); // HELLO, WORLD!

Switch Expressions (from JEP 325 - Preview Feature)

Two new features were added to switch expressions:

You can see these new features in the code below.

Let’s start by writing a traditional switch statement:

private String traditionalSwitch(DAY_OF_WEEK dayOfWeek) {
    String result;
    switch (dayOfWeek) {
        case MONDAY:
        case TUESDAY:
        case WEDNESDAY:
        case THRUSDAY:
        case FRIDAY:
            result = "Weekday";
            break;
        case SATURDAY:
        case SUNDAY:
            result = "Weekend";
            break;
        default:
            result = "Invalid day!";
    }
    return result;
}

Now let’s refactor it so we can use the case value -> syntax:

private String newSwitchFeature1(DAY_OF_WEEK dayOfWeek) {
    String result;
    switch (dayOfWeek) {
        case MONDAY, TUESDAY, WEDNESDAY, THRUSDAY, FRIDAY -> result = "Weekday";
        case SATURDAY, SUNDAY -> result = "Weekend";
        default -> result = "Invalid day!";
    }
    return result;
}

Looks much better and easier to read. We can also have a body using the case value -> syntax:

case SATURDAY, SUNDAY -> {
  result = "Weekend";
  System.out.println("Weekend");
} 

Inside each case statement we are assigning a value to a variable and returning it at the end of the method. We can simplify this code a little bit more:

private String newSwitchFeature2(DAY_OF_WEEK dayOfWeek) {
    var result = switch (dayOfWeek) {
        case MONDAY, TUESDAY, WEDNESDAY, THRUSDAY, FRIDAY -> "Weekday";
        case SATURDAY, SUNDAY -> "Weekend";
    };
    return result;
}

And if we don’t want to assign the switch to a variable, we can also return it directly:

private String newSwitchFeature2(DAY_OF_WEEK dayOfWeek) {
    return switch (dayOfWeek) {
        case MONDAY, TUESDAY, WEDNESDAY, THRUSDAY, FRIDAY -> "Weekday";
        case SATURDAY, SUNDAY -> "Weekend";
    };
}

Compared to the traditional switch, this is a nice refactoring!

As this is a preview feature, you’ll need to enable preview features in your favorite IDE as well in order to be able to compile and run the code:

Conclusion

These are very two simple features, which makes easier for us, developers, to keep ourselves up to date with latest Java features. Now let’s wait for Java 13 (September 2019) with more new features!

View the full source code on GitHub.

References:

Happy Coding!