About Question enthuware.ocpjp.v21.2.3800 :
Posted: Tue Nov 26, 2024 1:07 am
public int switchTest(byte x){
return switch(x){ //1
case 'b', 'c' -> 10; //2
case -2 -> 20; // 3
case 80, default -> 30; // 4
}; }
The explanation given for the above expression says that compilation will fail because of line 4. Because default cannot be combined with another case expression (case 80). However, the switch expression below was marked as a valid option. Considering the fact that the option also combines case null and default makes it confusing. Could you please explain? Thanks
String winLoseOrTie(Object obj){
return switch(obj){
case Integer i
when i<10 -> "lose";
case Integer i
when i>10 -> "win";
case Integer i
when i==10 -> "tie";
case null, default -> "invalid intput";
}; }
return switch(x){ //1
case 'b', 'c' -> 10; //2
case -2 -> 20; // 3
case 80, default -> 30; // 4
}; }
The explanation given for the above expression says that compilation will fail because of line 4. Because default cannot be combined with another case expression (case 80). However, the switch expression below was marked as a valid option. Considering the fact that the option also combines case null and default makes it confusing. Could you please explain? Thanks
String winLoseOrTie(Object obj){
return switch(obj){
case Integer i
when i<10 -> "lose";
case Integer i
when i>10 -> "win";
case Integer i
when i==10 -> "tie";
case null, default -> "invalid intput";
}; }