The evolution of Switch Statement up-to Java 14

Photo by Tim Mossholder on Pexels.com

Switch expressions in java are used to evaluate results or compute logic based on different values of a particular variable.This post looks at the evolution of switch statements up-to Java 14.

Until Java 7 only integers could be used in switch case and this had been the standard for a long time:

int value = 5;
switch(value){

    case 1:
        System.out.println("One");
        break;
    case 5:
        System.out.println("five");
         break;
    default:
        System.out.println("Unknown");
}

And then strings were introduced in case values and switch statements started to evolve.

String day = "Tuesday";
switch(day){

    case "Monday":
        System.out.println("Week day");
        break;
    case "Tuesday":
        System.out.println("Week day");
         break;
    default:
        System.out.println("Unknown");
}

Java 12 further enhanced the switch statement and introduced switch expressions as a preview feature. Preview features cannot be compiled and executed the usual way. They should be compiled and executed using –enable-preview option:

To compile:

javac –enable-preview –release 12 ClassName.java

To execute

java –enable-preview ClassName

To enable preview feature via eclipse check the below highlighted property in the latest version of eclipse with support plugin for Java 14 installed:

Coming back to switch expressions ,

Switch statements do not return a value back.

Switch expressions return a value back.

In java 12 you could do this :

String day = "Tuesday";
String result = switch(day){

    case "Monday":
       
        break "Week day";
    case "Tuesday":
         break "Week day";

    default:
        break "Unknown";
}

The word “break” can be used to return the result value.

This word was replaced by “yield” later in Java 13.

Also starting Java 12 multiple case values could be provided in a single case statement:

String day = "Tuesday";
String result = switch(day){

    case "Monday","Tuesday":   
      
         break "Week day";

    default:
        break "Unknown";
}

The case values can be separated by comma as shown above.

Further instead of returning values using break keyword , Java 12 introduced arrow operators as a simple alternative:

String day = "Tuesday";
String result = switch(day){

    case "Monday","Tuesday" ->Week day";

    default -> "Unknown";
}

“break” statements are not required if arrow operator is used.

Java 13 replaced the “break” keyword with “yield” for returning values in switch expressions:

String day = "Tuesday";
String result = switch(day){

    case "Monday","Tuesday":   
      
         yield "Week day";

    default:
        yield "Unknown";
}

Java 14 just made all the features permanent from being a preview feature.

The flag –enable-preview need not be set starting java 14.

And so switch statements have evolved into switch expressions!


Posted

in

by

Tags:

Comments

One response to “The evolution of Switch Statement up-to Java 14”

Leave a Reply

Discover more from The Full Stack Developer

Subscribe now to keep reading and get access to the full archive.

Continue reading