In this java article, we will be looking SWITCH statement in Java. SWITCH is used to simplify testing against multiple potential matches.
There are some limitations with SWITCH. Only primitive types like char and int can be used.
Lets look to syntax of SWITCH statement in Java.
int x = 2;
switch (x*1)
{
case 0:
System.out.println("result is 0");
case 1:
System.out.println("result is 1");
case 2:
System.out.println("result is 2");
}
Lets see it in Java array example.
In above example, we declared a variable "x" as type of int and we multiply it with 1 for test purpose. SWITCH relevant case block gets executed depend on result. Our result switch test result was "2", and as its expected, "case 2" has been executed to print a text.