In a switch statement in java, is it necessary that the "default" case be the last one? For example, can I do something like the following:
switch(x) {
case A: ....;
default: ....;
case B: ....;
}
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With