Must the "default" case come last in a switch?
No.. But it is suggested to put it at the end to make the code more readable. The code shown below works fine.
public static void main(String[] args) {
int i = 5;
switch (i) {
default:
System.out.println("hi");
break;
case 0:
System.out.println("0");
break;
case 5:
System.out.println("5");
break;
}
}
O/P : 5
no, the default statement could also be the first one.