int day =4;switch(day){case6:System.out.println("Today is Saturday");break;case7:System.out.println("Today is Sunday");break;default:System.out.println("Looking forward to the Weekend");}// Outputs "Looking forward to the Weekend"
int age =9;switch(age){case1:System.out.println("You are 1 year old");case5:System.out.println("You are 5 years old");case9:System.out.println("You are 9 years old");default:System.out.println("Ain't know how many years you are");
Aswitch works withthebyte,short,char, and int primitive data types.
It also works withenumerated types (discussed in EnumTypes), the Stringclass, and a few special classes that wrap certain primitive types:Character,Byte,Short, and Integer(discussed in Numbers and Strings).
int x =5;switch(x){case1:System.out.println("You entered 1");break;case2:System.out.println("You entered 2");break;default:System.out.println("You entered: "+x);}
int day =4;switch(day){case6:System.out.println("Today is Saturday");break;case7:System.out.println("Today is Sunday");break;default:System.out.println("Looking forward to the Weekend");}// Outputs "Looking forward to the Weekend"