wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Wrapper class variable in switch Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Wrapper class variable in switch" Watch "Wrapper class variable in switch" New topic
Author

Wrapper class variable in switch

Vijay Chandran
Ranch Hand

Joined: Jan 07, 2007
Posts: 178


Dear friends,

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.

Please explain me why this happened.

Thanks in advance.
Regards,
Vijay
Pho Tek
Ranch Hand

Joined: Nov 05, 2000
Posts: 761

autoboxing
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

JLS - 14.11 The switch Statement...
SwitchStatement:
switch ( Expression ) SwitchBlock

...

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
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
}

return found:
}
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19682
    
  20

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
}

return found:
}

You forgot the break statements.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Vijay Chandran
Ranch Hand

Joined: Jan 07, 2007
Posts: 178
Thanks a lot for the reply.

Sorry for the delayed thanks!! Just now checked the forum.


Regards,
Vijay
Nicholas Jordan
Ranch Hand

Joined: Sep 17, 2006
Posts: 1282
[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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Wrapper class variable in switch