Consider this code:
int num = 0; switch(num) { case 1: boolean bool = false; break; case 2: String one; String two; String three;
Since we were allowed to refer to a variable declared in another case, this means that even if case 1 not selected, boolean bool was still declared.
Since default is the last option, and java works from left to right (top to bottom), I assume that variables in case 2 (and any other cases) will also be declared.
This makes me think that the more code you have in cases declared before the selected case, the longer it will actually get access to this case compared to the fact that the first case was declared first.
Are there any specific reasons switch statements work with? And would it not be better to use if-else instead of switching with operators if there are many cases? (call processing time, nanoseconds)
source share