Forums Register Login

Case with unboxing

+Pie Number of slices to send: Send
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 .
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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!
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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!
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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 ]
Please do not shoot the fish in this barrel. But you can shoot at this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 916 times.
Similar Threads
Switch And Primitive Wrappers
Can someone tell me why?
switch() construct
Switch case
I want to insert Data into the pie chart.I have put the code to gernate the Pie
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 08:44:42.