Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Wrapper Classes for Switch

 
susan waters
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 198
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
susan waters
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Dinuka Arsakularatne
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your code compiles fine on my machine.
 
Waclav Pshedujko
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic