int day = 4;
switch (day) {
case 6:
System.out.println("Today is Saturday");
break;
case 7:
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){
case 1:
System.out.println("You are 1 year old");
case 5:
System.out.println("You are 5 years old");
case 9:
System.out.println("You are 9 years old");
default:
System.out.println("Ain't know how many years you are");
A switch works with the byte, short, char, and int primitive data types.
It also works with enumerated types (discussed in Enum Types), the String
class, and a few special classes that wrap certain primitive types:
Character , Byte , Short , and Integer (discussed in Numbers and Strings).
class Main {
public static void main(String[] args) {
int expression = 9;
switch(expression) {
case 2:
System.out.println("Small Size");
break;
case 3:
System.out.println("Large Size");
break;
// default case
default:
System.out.println("Unknown Size");
}
}
}
int day = 4;
switch (day) {
case 6:
System.out.println("Today is Saturday");
break;
case 7:
System.out.println("Today is Sunday");
break;
default:
System.out.println("Looking forward to the Weekend");
}
// Outputs "Looking forward to the Weekend"