Saturday 30 January 2021

Java12: Switch expression(Preview mode)

Java12 introduces switch expression feature in preview mode. From Java12 onwards, you can use switch statement either as a statement or an expression.

 

switch expressions bring clear and safe code. Let us see it with an example.

 

Prior to Java12

switch (number) {
	case 2:
		System.out.println("Number is even and prime");
		break;
	case 3:
	case 5:
		System.out.println("You choosen a prime number and it is < 7");
		break;
	case 4:
	case 6:
		System.out.println("You entered even number and it is < 8.");
	
	default:
		System.out.println("You entered a number");
}

 

From Java12 onwards, you can use switch as expression like below.

 

String result = switch (number) {
	case 2 ->  "Number is even and prime";
	case 3, 5 -> "You choosen a prime number and it is < 7";
	case 4, 6 -> "You entered even number and it is < 8.";
	default -> "You entered a number";
};

Find the below working application.

 

SwitchExpressionExample.java

public class SwitchExpressionExample {
	public static void main(String args[]) {
		int number = 2;

		// Prior to Java12
		switch (number) {
		case 2:
			System.out.println("Number is even and prime");
			break;
		case 3:
		case 5:
			System.out.println("You choosen a prime number and it is < 7");
			break;
		case 4:
		case 6:
			System.out.println("You entered even number and it is < 8.");

		default:
			System.out.println("You entered a number");
		}

		// From Java12
		String result = switch (number) {
			case 2 ->  "Number is even and prime";
			case 3, 5 -> "You choosen a prime number and it is < 7";
			case 4, 6 -> "You entered even number and it is < 8.";
			default -> "You entered a number";
		};
		
		System.out.println(result);
	}

}


How to compile SwitchExpressionExample application?

javac --release 12 --enable-preview SwitchExpressionExample.java

 

How to run SwitchExpressionExample?

java --enable-preview SwitchExpressionExample

 

Output

Number is even and prime

Number is even and prime

 

If your case statement has multiple lines, then enclose them in curly braces {} and return the last line using break statement.

 

SwitchExpressionExample2.java

public class SwitchExpressionExample2 {
	public static void main(String args[]) {
		int number = 2;

		
		String result = switch (number) {
		case 2 -> {
			System.out.println("For input 2");
			break "Number is even and prime";
		}
		case 3, 5 -> {
			System.out.println("For input 3 or 5");
			break "You choosen a prime number and it is < 7";
		}
		case 4, 6 -> {
			System.out.println("For input 4 or 6");
			break "You entered even number and it is < 8.";
		}
		default -> {
			System.out.println("No input matched");
			break "You entered a number";
		}
	};

		System.out.println(result);
	}

}


Output

$javac --release 12 --enable-preview SwitchExpressionExample2.java 
Note: SwitchExpressionExample2.java uses preview language features.
Note: Recompile with -Xlint:preview for details.
$
$
$java --enable-preview SwitchExpressionExample2
For input 2
Number is even and prime


Reference

https://openjdk.java.net/jeps/325

 





 

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment