I met this variations of switch statement, and I wondering to know in witch cases it can be used?
Strangeness is because it doesn't have cases before default value, only after. And doesn't have break
statement. But works fine.
Here is code:
public static Item newItem() {
switch (rand.nextInt(3)) {
default:
case 0:
return new Scissors();
case 1:
return new Paper();
case 2:
return new Rock();
}
}
How does it really can be execute at this strange style. I tried to make easy debugging all works fine it return accuracy one explicit instance.
Question: why does we need write:
default:
case 0:
instead typical usage:
case 0:
default:
Compiler didn't let to change or comment default
statement.
Any suggestions?
PS please, don't write silly explanation how does switch
works. it's clear. write only about this explicit example.
BTW I met this peace of code at Thinking in Java by Bruce Eckel.