• 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

does switch take a Integer wrapper?

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


how does this compile? switch can only take primitives(that can be implicitly casted to int) and enum right....
 
Ranch Hand
Posts: 453
Google Web Toolkit Hibernate Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
have you heard about autoboxing and unboxing .???

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

Unlike if-then and if-then-else, the switch statement allows for any number of possible execution paths. A switch works with the byte, short, char, and int primitive data types. It also works with enumerated types (discussed in Classes and Inheritance) and a few special classes that "wrap" certain primitive types: Character, Byte, Short, and Integer (discussed in Simple Data Objects ).



I read it here
 
Ankur kothari
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yup...initially i thought it can work that way here..through unboxing...but in K&B it isnt mentioned that wrappers are allowed...nor in moghuls book
 
avi sinha
Ranch Hand
Posts: 453
Google Web Toolkit Hibernate Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ankur kothari wrote:yup...initially i thought it can work that way here..through unboxing...but in K&B it isnt mentioned that wrappers are allowed...nor in moghuls book



you have got it wrong.

note these points

1> wrappers can be used in the switch expression. like switch( i ) where i is an Integer.

2> a wrapper can't be used in the case expression.

avi sinha
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also another thing i got to know is
Case expressions cannot be wrappers as Avi sinha said. It can neither be regular primitive type as well. But we can specify Compile time constants (Initialed Final variables along with declaration) and it cannot be just final variables.

Below one works

....
final int j = 3; //This works as its a compile time constant
switch(i) {
case j: System.out.println("three”"); break;
default: System.out.println("other”"); break;
}


Where as below one doesnot work and it gives compile time error.
final int j;
j = 3;
switch(i) {
case j: System.out.println("three”"); break;
default: System.out.println("other”"); break;
}







 
avi sinha
Ranch Hand
Posts: 453
Google Web Toolkit Hibernate Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
for a variable to be a compile time constant it should be initialized on the same line as that of declaration .

avi sinha
reply
    Bookmark Topic Watch Topic
  • New Topic