Skip to main content

Things changed from Java 11 - 17

·582 words·3 mins

Java 12
#

Switch Expressions (Preview) JEP 325
#

More on this in Switch Expression

Java 13
#

Switch Expressions (Second Preview) JEP 354
#

More on this in Switch Expression

Java 14
#

Switch Expression
#

Below are the changes in the switch construct introduced in Java 14, formalized by JEP 361, JEP-354, and JEP-325. The new Switch Expression addresses three main issues with the traditional switch statement: verbosity, variable scope issues, and being limited to a statement.

1. Its unnecessarily verbose and error prone
#

The traditional switch required break statements, leading to repetitive code and the potential for fall-through errors if a break was missed.

  • Problem Example (Traditional Switch Statement):

    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;
    }
    
  • Solution (Switch Expression with ->):

    The new syntax uses case L ->, which eliminates the need for an explicit break and allows multiple labels to be grouped.

    switch (weekDay) {
        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);
    }
    

2. Scope of variable within Switch
#

A variable declared in one case block in the traditional switch is scoped to the entire switch block, causing errors if the same variable name is used in another arm.

  • Problem Example (Variable Scope):

    switch (day) {
        case MONDAY:
        case TUESDAY:
            int temp = ...;     
            break;
        case WEDNESDAY:
        case THURSDAY:
            int temp = ...;    // Can't call this variable 'temp'
            break;
        default:
            int temp = ...;    // Can't call this variable 'temp'
    }
    

    Error: Variable ’temp’ is already defined in the scope

  • Workaround: The common workaround was declaring a variable outside the switch and assigning values inside each case.

    int temp;
    switch (day) {
        case MONDAY:
        case FRIDAY:
        case SUNDAY:
            temp = 6;
            break;
        case TUESDAY:
            temp = 7;
            break;
        case THURSDAY:
        case SATURDAY:
            temp = 8;
            break;
        case WEDNESDAY:
            temp = 9;
            break;
        default:
            throw new IllegalStateException("Wat: " + day);
    }
    
  • Solution: Using yield or Expression Form: The new switch can return a value, naturally handling scope.

    • Using yield (with :): The yield keyword is used to produce a value from the switch expression.
    Integer temp = switch (weekDay) {
                case MONDAY, FRIDAY, SUNDAY: 
                    yield 6;
                case TUESDAY:
                    yield 7;
                case THURSDAY, SATURDAY:
                    yield 8;
                case WEDNESDAY:
                    yield 9;
            };
    
    • Using -> (as expression): This concise syntax implicitly yields the result.
    Integer temp = switch (weekDay) {
                case MONDAY, FRIDAY, SUNDAY -> 6;
                case TUESDAY                -> 7;
                case THURSDAY, SATURDAY     -> 8;
                case WEDNESDAY              -> 9;
            };
    

3. Limited to a statement not expression
#

The traditional switch was limited to a statement and could not compute and return a single value directly.

  • As Statement (Original):

    int numLetters;
    switch (day) {
        case MONDAY:
        case FRIDAY:
        case SUNDAY:
            numLetters = 6;
            System.out.println(6);
            break;
        case TUESDAY:
            numLetters = 7;
            System.out.println(6);
            break;
        case THURSDAY:
        case SATURDAY:
            numLetters = 8;
            System.out.println(6);
            break;
        case WEDNESDAY:
            numLetters = 9;
            System.out.println(6);
            break;
        default:
            throw new IllegalStateException("Wat: " + day);
    }
    
  • As Expression (New Feature): The switch can now be used as an expression, where its result can be directly used in a statement like System.out.println().

    System.out.println(
            switch (weekDay) {
                case MONDAY, FRIDAY, SUNDAY -> 6;
                case TUESDAY -> 7;
                case THURSDAY, SATURDAY -> 8;
                case WEDNESDAY -> 9;
            }
    );
    

Java 15
#

Java 16
#

Java 17
#

Some important links#

JEPLink
JEP-354https://openjdk.org/jeps/354
JEP-325https://openjdk.org/jeps/325
JEP 361https://openjdk.org/jeps/361
Amar Singh
Author
Amar Singh
A little bit about you