Must the "default" case come last in a switch?
Asked Answered
E

4

18

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: ....;
}
Eleonoraeleonore answered 4/5, 2015 at 7:30 Comment(6)
Next time you come across a question like this, I recommend you try it out first.Rayner
Not sure why this is posted as a question when you can try it out yourself?Avrilavrit
The JLS does not put any restriction where to put the default case as it's a SwitchLabel like any other. The only restriction is that you cannot have more than 1 default case in the same switch statement.Erdmann
Of course I could try it out. I posted it as a question for the community. Geez.Eleonoraeleonore
Arrived here by Googling this question. Thanks @KelseyRiderAsir
I tried it myself but still googled this to see what others have to say about this.. Thank you @EleonoraeleonoreKila
C
24

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
Cathrine answered 4/5, 2015 at 7:32 Comment(1)
in some rare cases it could be more readable to place it first (or not last), e.g. if you have lots of switch statements that return a boolean (true/false) where you want the 1st return to be false and the 2nd true for consistency across all statementsPeatroy
A
6

no, the default statement could also be the first one.

Aceto answered 4/5, 2015 at 7:31 Comment(0)
W
2

It surely is not necessary, but beware it will behave a lot differently in case fall-through is used.

int[] test = {0,1,5,23,24};
for (int t: test) {
    System.out.println(String.format("Running test: %s", t));
    switch(t) {
    case 0:
        System.out.println(String.format("Match: %s", 0));
    default:
        System.out.println(String.format("Match: %s", "default"));
    case 23:
        System.out.println(String.format("Match: %s", 23));
    }
}

Prints:

Running test: 0
Match: 0
Match: default
Match: 23
Running test: 1
Match: default
Match: 23
Running test: 5
Match: default
Match: 23
Running test: 23
Match: 23
Running test: 24
Match: default
Match: 23

This simply speaking means:

  • if there is an exact (non-default) case defined after default case it is matched first instead of the default one
  • if there is not, then the default case understandably makes all tested values fall through from itself down

Observe especially the test for 23 - the result would be different if case 23 and default was swapped (both will be matched then).

Demo: https://ideone.com/LyvjXQ

Wanonah answered 10/11, 2022 at 12:26 Comment(0)
C
-2

The default statement can be placed anywhere inside switch statement body

   switch (i) {

          // the default statement can be placed anywhere inside the switch statement body

    }
Cindiecindra answered 6/5, 2020 at 20:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.