Hello everyone!
I`m here at your mercy hoping someone will pour some light on my ternary operator problem.
As specified by language, ternary operator requires either of its latter 2 operands to pass IS-A
test.
Consider the following ( ugly ) code:
When compiled with
javac -source 1.4 Test.java
the code doesn't compile and compiler rightfully complains that neither Double nor Integer are subtype of another.
However, when compiled as 1.6 or as 1.5 source, it compiles fine, runs and always returns objects of type Double.
For example the code above outputs "We got a Double and its value is 10.0"
The question:
Which feature of
Java 5 is at work here?
I know its not auto-boxing ( since auto-boxing only deals with boxing and un-boxing primitives where due ) or at least not only. IMHO the code shouldn't compile at all, since there is no way Integer can be cast to Double, and the types of ternary operator operands are specified explicitly as Integer and Double.
My own guess is Integer could be un-boxed to int, implicitly widened to double and then boxed into Double.
[edit]Newlines because lines off edge of screen. CR[/edit]