I am a little puzzled by the release of this program:
public class xx { public static void main(String[] args) throws Exception { Number x = false ? new Long(123) : new Integer(456); System.out.println(x + " isa " + x.getClass().getName()); } }
Here it outputs:
456 isa java.lang.Long
It seems that the compiler "pushes" an object of type Integer to Long , just as it usually pushes primitive values. I have never heard of the promotion of objects, and this behavior seems very unexpected.
My question is: is this really the correct behavior according to JLS? If so, I would like to see a link, if possible.
Or is this some kind of autoboxing-off-wild compiler error?
I use:
java version "1.8.0_60" Java(TM) SE Runtime Environment (build 1.8.0_60-b27) Java HotSpot(TM) 64-Bit Server VM (build 25.60-b23, mixed mode)
source share