• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

switch case and wrapper objects

 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi

I was trying out this code


Why is j not acceptable as a valid case, since it is final (just as i is)?

Thanks
Devi
 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Devi,

The only type that a switch can evaluate is the primitive int. The values that can be promoted to an int, that is byte, short, char are also acceptable. Here you are trying to match an Integer Wrapper object to the switch statement. That is why you are getting that error.

I hope this explains your doubt. And you rest of you guys.... correct me if I am wrong.

Swapna.
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sounds correct to me Swapna.

The only other thing that Devi could do is to type in the switch statement to get a primitive int.

Sok
 
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

Sorry Stephen..

it gives a compiler error...

case expressions must be constant expressions...

what does that mean???

Regards
 
Swapna James
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Guys,

An excerpt from K&B book chapter 4:


The only argument a case can evaluate is one of the same type as switch can use,with one additional�and big�constraint: the case argument must be final! The case argument has to be resolved at compile time, so that means you can use only a literal or final variable.



Hope this clears your doubt.

Swapna.
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

case argument can only take a literal or final variable.

eg 1:
byte a = 20;
switch(a){
case 20:
{
System.out.println("20");
break;
}
case 127:
{
System.out.println("127");
break;
}
}


eg 2:
final int one = 1;
final int two = 2;
int a = 2;
switch(a){
case one:
{
System.out.println("one");
break;
}
case two:
{
System.out.println("two");
break;
}
}eg 1:
 
Sreedevi Vinod
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That is not right. The switch statement does allow wrappers of the numeric types, since autoboxing is now supported. Then why doesn't the case statement take a wrapper type as argument when it is declared final and hence is a constant ?

Thanks
Devi
 
Ranch Hand
Posts: 193
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also, remember that Java 5 adds enums as valid arguments to case statements.

Details here:
http://java.sun.com/j2se/1.5.0/docs/guide/language/enums.html

Josh
 
Sreedevi Vinod
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What I mean is that the below code allows t to be an Integer, but will not compile if I make j an Integer ( though j is final). Any ideas ?

Thanks
Devi
[ September 01, 2005: Message edited by: Sreedevi Vinod ]
 
Ranch Hand
Posts: 657
Spring VI Editor Clojure
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

What I mean is that the below code allows t to be an Integer, but will not compile if I make j an Integer ( though j is final). Any ideas?


The "case values" must be constant expressions, which by definition do not include values of Integer types.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic