We can pass primitive values like int,char to switch statement. But this code passes an object of Integer (wrapper) variable to the switch statement. The compiler didn't complain anything about this. It executed perfectly.
The type of the Expression must be char, byte, short, int, Character, Byte, Short, Integer, or an enum type...
"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer sscce.org
Nicholas Jordan
Ranch Hand
Joined: Sep 17, 2006
Posts: 1282
posted
0
boolean testIfFound(Integer i) { boolean found; final int switchValue = i.intValue(); switch(i) { case 0: found = true; case 1: found = true; // ..... and so on // ..... and so on // ..... and so on // ..... and so on // ..... and so on // ..... and so on }
Originally posted by Nicholas Jordan: boolean testIfFound(Integer i) { boolean found; final int switchValue = i.intValue(); switch(i) { case 0: found = true; case 1: found = true; // ..... and so on // ..... and so on // ..... and so on // ..... and so on // ..... and so on // ..... and so on }
Sorry for the delayed thanks!! Just now checked the forum.
Regards, Vijay
Nicholas Jordan
Ranch Hand
Joined: Sep 17, 2006
Posts: 1282
posted
0
[Rob Prime:] You forgot the break statements.
Yes, I did - but the switch I wrote will cascade along the steppes and could have as well have been written:
Switch cases do not necessarily need the breaks, in this one the cascading down the chain just wastes a few dozen processor cycles ~ the effect is the same. [ September 21, 2007: Message edited by: Nicholas Jordan ]