New Feature of JDK13

New Features of JDK13

1.Switch Expressions(Preview)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public class SwitchDemo {
public static void main(String[] args) {
int month = 1;
int year = 2001;

// switch expressions can be used in complex expressions:
int minutes = 24 * switch (month) {
case 1, 3, 5, 7, 8, 10, 12 -> 31;
case 4, 6, 9, 11 -> 30;
case 2 -> isLeapYear(year) ? 29 : 28;
default -> throw new IllegalArgumentException("The month should in range(1~12)!");
} * 60;

System.out.println(String.format("There are %d minutes in month %d, year %d.", minutes, month, year));

}

private static boolean isLeapYear(int year){
return year % 400 == 0 || (year % 100 != 0 && year %4 == 0);
}
}

The output of above code:

switch_expression

2.Text Blocks (Preview)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class MultiLineStringDemo {
public static void main(String[] args) {
String multiLine = """
Down by the salley gardens
My love and I did meet
She passed the salley gardens
With little snow-white feet
She bid me take love easy
As the leaves grow on the tree
But I being young and foolish
With her would not agree
""";

multiLine.lines().map(s -> s + "🎵").forEach(System.out::println);
}

}

The output of above code:

multilinestring