This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Case with unboxing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Case with unboxing" Watch "Case with unboxing" New topic
Author

Case with unboxing

bernard savary
Ranch Hand

Joined: Jan 09, 2008
Posts: 91
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

Joined: Aug 14, 2006
Posts: 107
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

Joined: Jan 09, 2008
Posts: 91
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

Joined: Apr 29, 2008
Posts: 47
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

Joined: Mar 22, 2008
Posts: 265

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.


There will always be people who are ahead of the curve, and people who are behind the curve. But knowledge moves the curve. --Bill James
Seema Gaurav
Ranch Hand

Joined: Apr 29, 2008
Posts: 47
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

Joined: Mar 22, 2008
Posts: 265

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

Joined: Oct 13, 2005
Posts: 37884
    
  22
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

Joined: Mar 22, 2008
Posts: 265

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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Case with unboxing
 
Similar Threads
Switch And Primitive Wrappers
I want to insert Data into the pie chart.I have put the code to gernate the Pie
switch() construct
Can someone tell me why?
Switch case