Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVA

java switch expression produce result

// Use the yield keyword to produce a result in a switch expression.

int result = switch (month) {
    case JANUARY, JUNE, JULY -> 3;
    case FEBRUARY, SEPTEMBER, OCTOBER, NOVEMBER, DECEMBER -> 1;
    case MARCH, MAY, APRIL, AUGUST -> {
        int monthLength = month.toString().length();
        yield monthLength * 4;
    }
    default -> 0;
};
Source by www.baeldung.com #
 
PREVIOUS NEXT
Tagged: #java #switch #expression #produce #result
ADD COMMENT
Topic
Name
1+8 =