Thursday, March 21, 2019

JDK 12 Switch Expressions


SWITCH -

Enhance the Java programming language to support pattern matching concepts using in Switch Expression


Look in to this below switch statement
Old Programming
switch (day) {
    case MONDAY:
    case FRIDAY:
    case SUNDAY:
        System.out.println(6);
        break;
    case TUESDAY:
        System.out.println(7);
        break;
    case THURSDAY:
    case SATURDAY:
        System.out.println(8);
        break;
    case WEDNESDAY:
        System.out.println(9);
        break;
}
JDK 12 introduce a new form of switch label,
written "case L ->" to signify that only the code to the right of the label is to be executed if the label is matched.
For example
Previous code can now be written:
New Programming
switch (day) {
    case MONDAY, FRIDAY, SUNDAY -> System.out.println(6);
    case TUESDAY                -> System.out.println(7);
    case THURSDAY, SATURDAY     -> System.out.println(8);
    case WEDNESDAY              -> System.out.println(9);
}

No comments:

Post a Comment