top of page

brace yourselves java 17 is coming (1/6)

Updated: May 24, 2021

hands on: java 12


like a lot of you already know, java 17 is about the corner. since java 9 oracle releases a non-lts (long term support) version every 6 months with some features. in java 17 all these features will be finalized. this version will be the next jdk with long term support, next to 8 and 11. so what changes happend since the last lts openjdk 11? showing all features in one article can be a bit overwhelming, that's why will split it up to separate blog entries, beginning with version 12. i will present features for programming only including preview features. i won't show any garbage collector news or anything similar.


switch expressions

with java 12 a new way to write switch expression was introduced. you can now directly return the value of a switch case expression, instead of storing it in a temporary variable or providing return values for each switch case. another change is that the break keyword was renamed to yield. the next advantage is the less verbose writing style. now multiple values of the same case can be written comma separated, instead of writing one case per line. additionally the compiler stops you from shipping new code, if not all branches were covered of the value under control of the switch case. the following example uses the new annotation style, the enum consists of 12 values, which all have to be covered in the case tree. if one month went missing, you'll get a compile error. the same thing happens, when the value is a string or an integer. one cannot possibly cover all int values. the compiler, or in my case intellij, tells you to add the default branch. lets see how the new style looks like.

public static int getSeason(final Month month) {
    return switch (month) {
        case JANUARY, FEBRUARY, MARCH -> 1;
        case APRIL, MAY, JUNE -> 2;
        case JULY, AUGUST, SEPTEMBER -> 3;
        case OCTOBER, NOVEMBER, DECEMBER -> 4;
    };
}

thanks to this syntax, switch cases will become more popular again. there is also another approach writing switch cases using the yield keyword. it's obviously more verbose and reminds of the older style.

public static List<Month> getMonths(final int season) {
    return switch (season) {
        case 1 -> {
            System.out.println(1);
            yield List.of(Month.JANUARY, Month.FEBRUARY, Month.MARCH);
        }
        case 2 -> {
            System.out.println(2);
            yield List.of(Month.APRIL, Month.MAY, Month.JUNE);
        }
        case 3 -> {
            System.out.println(3);
            yield List.of(Month.JULY, Month.AUGUST, Month.SEPTEMBER);
        }
        case 4 -> {
            System.out.println(4);
            yield List.of(Month.OCTOBER, Month.NOVEMBER, Month.DECEMBER);
        }
        default -> throw new IllegalArgumentException("unknown season");
    };
}

the advantage of the new syntax is, that you are forced to use yield. you can't forget the keyword like you may using break with the old annotation style.


pattern matching

everyone knows how painful it is to check an object for its instance and then cast it in the next line. there is no need for such a thing since java 12, you can do it in the same line.

public static void main(final String[] args) {
    final Object object = "value";
    
    if (object instanceof String string) {
        System.out.println(string.substring(1));
    }
}

this feature isn't a game changer but it helps reducing keystrokes and verbosity.


compact number format

with the number format class you can, like the class says, format numbers. usually one determines how many pre or post decimal places a number has, or in which locale the value should be represented. the new feature allows us to change the representation of a number equal or greater to thousand.

public static void main(final String[] args) {
    final var english = new Locale("en", "US");
    final var shortFormat = NumberFormat.getCompactNumberInstance(english, NumberFormat.Style.SHORT);
    shortFormat.setMaximumFractionDigits(2);

    final var longFormat = NumberFormat.getCompactNumberInstance(english, NumberFormat.Style.LONG);
    longFormat.setMaximumFractionDigits(2);

    System.out.println(shortFormat.format(1337));
    System.out.println(longFormat.format(1337));
}

the output of the print statements for a english locale will be the following:

1.34K
1.34 thousand

summary

all in all i like the new changes, the new syntax of the switch expressions and the pattern matching reduce key strokes and let the developer focus on the essentials of his work. specially the switch cases become less verbose and less error-prone. the developer cannot forget the yield keyword in any case, nor are any conditions not covered.


so what do you think about the changes? do you like it and will try to adapt them in upcoming process, or will you wait for the next lts before using it? let me know.


see the full source code here: https://github.com/KlemensM/java12

Recent Posts

See All
bottom of page