Sahil Kapoor wrote:
1) It would unbox switch expression ie new Integer(4) and compares it with int w.
2) It would Box w and then compares with the switch expression.
1 is true - it would unbox new Integer(4)
Look here in the specification:
When the switch statement is executed, first the Expression is evaluated. If the Expression evaluates to null, a NullPointerException is thrown and the entire switch statement completes abruptly for that reason. Otherwise, if the result is of a reference type, it is subject to unboxing conversion (§5.1.8). If evaluation of the Expression ....
But in accordance with docs , the following two usages of switch statement must be same , but indeed they are different , second program will show compile time error.
Program 1 :-
This program would compile fine. No problem !!!
Program 2:- This is essentially same the above code according to docs , but shows compile time error.
But but but, if the case would be like , case constants are boxed , ie option 2, then in accordance to it, the above two codes behaviour could be reasoned.
Sahil has put a good question the program 2 specified by him certainly looks like a benign java code that should run fine but it is throwing a compile time error
switchlochaTst.java:8: incompatible types
found : char
case 's': break;
I am quite perplexed by this error as according to JLS the wrapper object is unboxed to int and as 's' being a character it is implicitly cast to int so everything should work fine. So ranchers need your help in understanding the reason for the error
Thanks in advance
's' needs to be explicitly declared as an int or type Integer.
it is so because in the second switch statement we are not providing a constant as an expression but a reference to an Integer ... which certainly will look for integers only ... i think char is an int but not Integer type.
what are your views on this ranchers.
2. The switch expression is unboxed as given in the spec.
3. The spec also says that the case constants must be compatible with the switch expression. This is why the following code fails to compile
's' is not compatible with the switch expression (as Integer i = 's'; won't compile). Although the switch expression will be unboxed and after that 's' will be compatible with the unboxed value 30, but the compiler also have to ensure that the case constants are compatible with the switch expression before the unboxing takes place...
thanks for your explainations now the error makes sense . I tried out the same code on MyEclipse and surprisingly it works just fine . I just modified the code a little
the code compiles and runs giving the output " in default "
could you please throw some light on the reason for such change in behaviour when we run the same code using IDE and command prompt
thanks in advance
above code compile perfectly in sun compiler (javac) why above code works but not switch statement?
I understand switch statement always works on int, and the way I understand, it should first try to convert new Integer(30) to int, instead of trying to convert char 's' to Integer.
Ankit Garg wrote:When you use == operator, if one of the operands is a primitive and the other is a wrapper class, the wrapper is unboxed. So in your case i is unboxed to int, then a is promoted to int and then the conversion is done...
Ankit is right ...
wrapper in unboxed to the primitive type always ...
and since char is internally an int .. it is promoted to an int before == checking ...
in case of the switch statement ... since the actual expression is evaluated only before being converted or unboxed or promotion ... this happens
Lalit Mehra wrote:in case of the switch statement ... since the actual expression is evaluated only before being converted or unboxed or promotion ... this happens
I assume this is not correct behavior, even though we know it is behaving like this. I am trying to understand logic behind this behavior. Is there any logical reason why compiler behave like this.
from the logical thinking, I think switch as multiple if else, may be implementation wise there is difference the way if and switch statement converted into byte code.
Or may be I am thinking too much, I should accept this or move on?
Pradeep- Kumar wrote:from the logical thinking, I think switch as multiple if else, may be implementation wise there is difference the way if and switch statement converted into byte code.
Well yes logically you can think of switch as multiple if else. But the rules for if-else don't apply to switch. The case constants are not compared to the switch expression with == operator. Switch statement is a construct in itself. So the case constants need to be compatible with the switch expression, the JLS doesn't say that the case constants must be compatible with the switch expression *after* the switch expression is unboxed...