Why does the ternary operator unexpectedly cast integers?
问题 I have seen it discussed somewhere that the following code results in obj being a Double , but that it prints 200.0 from the left hand side. Object obj = true ? new Integer(200) : new Double(0.0); System.out.println(obj); Result: 200.0 However, if you put a different object in the right hand side, e.g. BigDecimal , the type of obj is Integer as it should be. Object obj = true ? new Integer(200) : new BigDecimal(0.0); System.out.println(obj); Result: 200 I presume that the reason for this is