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

does switch take a Integer wrapper?

 
Ankur kothari
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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

avi sinha
 
Neha Daga
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
 
satish kumar kandukuri
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic