Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Case with unboxing

 
bernard savary
Ranch Hand
Posts: 91
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,
I have the following code
1.Integer d = 10 ;
final short k = 100;
switch (d)
{
case k:
}
2.Short d = 10 ;
final int k = 100;
switch (d)
{
case k:
}
3. int d = 10 ;
final short k = 100;
switch (d)
{
case k:
}
The code#1 doesnt compile but code #2 and code#3 compiles. Can someone explain why is it so ? and what does the compiler do here .
 
Ian Edwards
Ranch Hand
Posts: 107
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The expression in a switch statement has to be something that is an integer type or can be promoted to an integer. Equally, the case label must be an integer type or enum constant. The case label must be assignable to the type of the switch expression.

In code sample 3 we can see that the expression is an int, and the case label is a short (which can be promoted to an int).

In code sample 2 the switch expression is the wrapper class Short and case label is an int. In this case the value of k (100) can fit into a short. If you change the value of k to 100000 then the compiler would complain that a Short is required but an int was found.

In the case of code sample 1 it appears the compiler has a problem as it can't widen and then box the short to an Integer.
 
bernard savary
Ranch Hand
Posts: 91
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was thinking the compiler would unbox the switch from integer to int ,as the switch value is compatible to int .I wonder why it doesnt do it here .
Anyway thanks a lot!
 
Seema Gaurav
Ranch Hand
Posts: 47
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bernard,
You are right in thinking that the compiler unboxes the Integer to an int, there is no problem there. However, the problem arises when you try to down cast 'int' within the switch expression to match a 'short' in the case(s).Since the case constant is a short and an int cannot automatically narrow down to a short, it throws a compiler error. Correct me if I'm wrong.
Hope that helps!
Seema Gaurav
 
Stevi Deter
Ranch Hand
Posts: 265
Hibernate Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by bernard savary:



The code#1 doesnt compile but code #2 and code#3 compiles. Can someone explain why is it so ? and what does the compiler do here .


What compiler error are you getting? What version of the JDK are you using? I'm able to compile that code just fine under Java 6.
 
Seema Gaurav
Ranch Hand
Posts: 47
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Stevi and Bernard,
I compiled the code using Java 1.6. Case 1 gave me the following error:
incompatible types
found: short
required: java.lang.Integer.

I changed case 1 to the following and it worked fine:
Integer d = 10 ;
final short k = 100;
switch (d)
{
case (int)k:
}
My explanation given earlier was based on this code. Correct me if I'm wrong.
Thanks,
Seema Gaurav
 
Stevi Deter
Ranch Hand
Posts: 265
Hibernate Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Very interesting. Eclipse reports this as fine, compiles, and even lets me run it.

But if I compile from the command line, I do indeed get the same compilation error as Seema.

Guess I'll be spending some time finding out why Eclipse is ignoring the problem. I definitely accept javac's compilation error as canonical over the IDE!
 
Campbell Ritchie
Sheriff
Pie
Posts: 47243
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to go to the Java Language Specification, where each type must be assignable to the type of the swtich expression. A Short isn't assignable to an Integer, so the Eclipse compiler is obviously being too lax.
 
Stevi Deter
Ranch Hand
Posts: 265
Hibernate Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is actually an interesting case.

There is an accepted bug (ID 6578574) against the compiler for this in the SDN bug database.

But as Campbell points out, the current javac behavior is per the JLS.

So, to quote the summary from Eclipse:

Bottom-line:
- the current behavior of javac matches the spec, which mandates an error;
- there is an accepted javac bug that asks for a the error be suppressed.


And I got to submit my first bug for Eclipse!
[ May 13, 2008: Message edited by: Stevi Deter ]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic