tertiary operator error casting Float to Object

Alex Buckley alex.buckley at oracle.com
Fri Sep 15 23:53:30 UTC 2023


On JDK 17.0.6, for example, I get the expected result (temp = 60.0) 
which is the correct result.

Which JDK version are you using?

(For the record: per JLS 15.25.2 and 5.6, the type of the numeric 
conditional expression `isInt ? ... : ...` is float, which is assignment 
compatible with Object.)

Alex

On 9/15/2023 4:21 PM, Liangtao wrote:
> The following code always output incorrect casting result:
> 
> // BEGIN Bug.java code
> public class Bug {
>      public static void main(String[] args) {
>          float temp = 60.0f;
>          boolean isInt = true;
>          Object o = isInt ? Integer.valueOf((int)temp) : 
> Float.valueOf(temp);
>          System.out.println("isInt " + isInt + ", temp = " + o);
>      }
> }
> // END Bug.java code
> 
> Steps to reproduce:
> $ javac Bug.java && java Bug
> 
> Expected Result:
> 
> isInt true, temp = 60.0
> 
> 
> Actual Result:
> 
> isInt true, temp = 60
> 
> 


More information about the compiler-dev mailing list