What is enhanced switch statement??? How Java 12 is going to change the way of writing Switch statement?
Oracle provided the Java 12 EA (Early Access) version before releasing Java 11.It shows that Oracle is committed to provide the best features to the developers asap. In order to make coding easy and more readable Java 12 is going to change the way we are writing switch statement. In the traditional way we write the code as I have written below.
public static void oldSwitchStatementDemo() {
System.out.println("Old Switch Statement:");
final int inputNumber = 3;
String resultStr;
switch (inputNumber){
case 1 :
resultStr = "ONE";
break;
case 2 :
resultStr = "TWO";
break;
case 3:
resultStr = "THREE";
break;
default:
resultStr = "N/A";
}
System.out.println("Input Number : " + inputNumber + " ==> " + resultStr);
}
But after the release of Java 11 we can write the in three different ways
1. Switch with return statement
public static void newSwitchExpressionWithBreaks(){
final int inputNumber = 1;
System.out.println("Switch Expression with Colons/Breaks:");
final String resultStr = switch (inputNumber) {
case 1 :
break "ONE";
case 2 :
break "TWO";
case 3 :
break "THREE";
default :
break "N/A";
};
System.out.println("Input Number : " + inputNumber + " ==> " + resultStr);
}
2. Switch with return statement via Arrow (->) operator
public static void demoSwitchExpressionWithArrowOperator() {
final int inputNumber = 3;
System.out.println("Switch Expression with Arrows:");
final String resultStr = switch (inputNumber){
case 1 -> "ONE";
case 2 -> "TWO";
case 3 -> "THREE";
case 4 -> "FOUR";
default -> "N/A";
};
System.out.println("Input Number : " + inputNumber + " ==> " + resultStr);
}
3. Switch with shared cases
public static void demoSwitchExpressionWithSharedCases(){
final int inputNumber = 7;
System.out.println("Shared/Multiple Case Labels:");
final String resultStr = switch (integer) {
case 0 -> "ZERO";
case 1, 3, 5, 7, 9 -> "ODD";
case 2, 4, 6, 8, 10 -> "EVEN";
default -> "N/A";
};
System.out.println("Input Number : " + inputNumber + " ==> " + resultStr);
}
public static void oldSwitchStatementDemo() {
System.out.println("Old Switch Statement:");
final int inputNumber = 3;
String resultStr;
switch (inputNumber){
case 1 :
resultStr = "ONE";
break;
case 2 :
resultStr = "TWO";
break;
case 3:
resultStr = "THREE";
break;
default:
resultStr = "N/A";
}
System.out.println("Input Number : " + inputNumber + " ==> " + resultStr);
}
But after the release of Java 11 we can write the in three different ways
1. Switch with return statement
public static void newSwitchExpressionWithBreaks(){
final int inputNumber = 1;
System.out.println("Switch Expression with Colons/Breaks:");
final String resultStr = switch (inputNumber) {
case 1 :
break "ONE";
case 2 :
break "TWO";
case 3 :
break "THREE";
default :
break "N/A";
};
System.out.println("Input Number : " + inputNumber + " ==> " + resultStr);
}
2. Switch with return statement via Arrow (->) operator
public static void demoSwitchExpressionWithArrowOperator() {
final int inputNumber = 3;
System.out.println("Switch Expression with Arrows:");
final String resultStr = switch (inputNumber){
case 1 -> "ONE";
case 2 -> "TWO";
case 3 -> "THREE";
case 4 -> "FOUR";
default -> "N/A";
};
System.out.println("Input Number : " + inputNumber + " ==> " + resultStr);
}
3. Switch with shared cases
public static void demoSwitchExpressionWithSharedCases(){
final int inputNumber = 7;
System.out.println("Shared/Multiple Case Labels:");
final String resultStr = switch (integer) {
case 0 -> "ZERO";
case 1, 3, 5, 7, 9 -> "ODD";
case 2, 4, 6, 8, 10 -> "EVEN";
default -> "N/A";
};
System.out.println("Input Number : " + inputNumber + " ==> " + resultStr);
}
Comments
Post a Comment