aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Wrapper Classes for Switch Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Wrapper Classes for Switch" Watch "Wrapper Classes for Switch" New topic
Author

Wrapper Classes for Switch

susan waters
Ranch Hand

Joined: Feb 21, 2006
Posts: 68
Hello All,
Can I use Wraper Integer, Byte, Character for Switch ? I know I need to use only int, byte, char. But with auto boxing feature in 5, Can I use these ? Thanks
Dinuka Arsakularatne
Ranch Hand

Joined: Aug 12, 2006
Posts: 198
you can use wrappers inside a switch statement. but if you are going to use wrappers in a case argument then remember they have to be compiletime constants. and also the best way to check this kind of doubt is to write some test code and test it yourself. it will help you remember even better.

hope this helps

Dinuka Arseculeatne


if who i am is what i have, and what i have is lost, then who am i?<br /> <br />SCJP 5.0<br />SCWCD 1.4<br />SCBCD preparing
susan waters
Ranch Hand

Joined: Feb 21, 2006
Posts: 68
Thanks. Right now I am at work and I do not have 5 compiler.
I will cehck the code when I go home with 5 compiler. Thanks for your reply.
Riyaz Saiyed
Greenhorn

Joined: Aug 13, 2006
Posts: 22
When i tried this, i got compile time error at first case condition.

final Integer condition = 7;
final int another = 2;
switch(condition) {
case condition:break; // Error: Case expression must be constant expression
case another:break; // OK: This is fine
}
Alangudi Balaji Navaneethan
Ranch Hand

Joined: Apr 28, 2004
Posts: 42
Yes,


I also tried with int and an Integer object. The expression is switch accepts it. But the case statement complains that the expression must be constant expression. I have tried as,

Integer in = new Integer(12);
switch(in)
{
case in: system.out.println(in.intValue());
case in.intValue(): System.out.println(in.intValue());
}
Both statements failed...


if you think you can you r right<br />if you think you can not you r double right
Dinuka Arsakularatne
Ranch Hand

Joined: Aug 12, 2006
Posts: 198
wrapper objects such as Integer cannot be used in a case statement as they are not compile time constants(because the boxing and un boxing happens at runtime). hence you can only use primitives that are compile time constants whic must also be final.

Dinuka Arseculeratne
Waclav Pshedujko
Greenhorn

Joined: Sep 28, 2006
Posts: 7
I have encounterd another problem with Wrapper Classes in Switch, that I do not understand:


Spec (14.11) says "...Every case constant expression associated with a switch statement must be assignable (5.2) to the type of the switch Expression."
so I would expect the above code to compile but it does not.
Using sun's jdk1.5.0_04
Petrus Pelser
Ranch Hand

Joined: Feb 20, 2006
Posts: 132
Your code compiles fine on my machine.
Waclav Pshedujko
Greenhorn

Joined: Sep 28, 2006
Posts: 7
Hm, yes, just tried to compile it from command line and it was ok.
Previously I was using eclipse, so the error message seems to be result of eclipse bug.
Thanks.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Wrapper Classes for Switch