January 28, 2020 /   java   java 13   java preview features   switch preview   text block preview

Java 13 Features

Java 13 was released with two preview features which will be available in Java 14 as new features. Text Block features looks really cool.

The Text Blocks

This preview feature resembles the python's text blocks, where we can write a big and new line separated string without concatenation operator (+)

Here is an example

public class Java13 {
    public static void main(String[] args) {
        String textBlock = """
                    Hello World.
                    This is Text Blocks test.
                    This is a cool feature.
                """;
        System.out.println(textBlock);
    }
}

This will output

    Hello World.
    This is Text Blocks test.
    This is a cool feature.

The yield in Switch Statement

In Java 12, it allows you to return with break statement. Here is how a switch statement can be written in Java 12

    int daysJava12 = switch (month) {
        case "January":
        case "March":
        case "May":
        case "July":
        case "August":
        case "October":
        case "December":
            break 31;
        case "April":
        case "June":
        case "September":
        case "November":
            break 30;
        case "February":
            break 29; // Talking about 2020 only.
        default:
            throw new IllegalArgumentException("I didn't here about that month !!!!");
    };

But overloading break as a return statement did not seem a good idea. So in Java 13 return is made with yield . Here is how you will will write a switch statement in java 13

    String month = "January";
    int daysJava13 = switch (month) {
        case "January":
        case "March":
        case "May":
        case "July":
        case "August":
        case "October":
        case "December":
            yield 31;
        case "April":
        case "June":
        case "September":
        case "November":
            yield 30;
        case "February":
            yield 29; // Talking about 2020 only.
        default:
            throw new IllegalArgumentException("I didn't here about that month !!!!");
    };