<div dir="ltr"><div dir="ltr">OpenJDK 17.0.8.1 also gets "60.0".<div><br></div><div>So it's actually "right" according to JLS. However, as a conservative guy I lose confidence of using tertiary operator now...</div></div><div><br></div><div>Thanks,</div><div>Liangtao</div><div><br></div><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Fri, Sep 15, 2023 at 6:51 PM Alex Buckley <<a href="mailto:alex.buckley@oracle.com">alex.buckley@oracle.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">On JDK 17.0.6, for example, I get the expected result (temp = 60.0) <br>
which is the correct result.<br>
<br>
Which JDK version are you using?<br>
<br>
(For the record: per JLS 15.25.2 and 5.6, the type of the numeric <br>
conditional expression `isInt ? ... : ...` is float, which is assignment <br>
compatible with Object.)<br>
<br>
Alex<br>
<br>
On 9/15/2023 4:21 PM, Liangtao wrote:<br>
> The following code always output incorrect casting result:<br>
> <br>
> // BEGIN Bug.java code<br>
> public class Bug {<br>
>      public static void main(String[] args) {<br>
>          float temp = 60.0f;<br>
>          boolean isInt = true;<br>
>          Object o = isInt ? Integer.valueOf((int)temp) : <br>
> Float.valueOf(temp);<br>
>          System.out.println("isInt " + isInt + ", temp = " + o);<br>
>      }<br>
> }<br>
> // END Bug.java code<br>
> <br>
> Steps to reproduce:<br>
> $ javac Bug.java && java Bug<br>
> <br>
> Expected Result:<br>
> <br>
> isInt true, temp = 60.0<br>
> <br>
> <br>
> Actual Result:<br>
> <br>
> isInt true, temp = 60<br>
> <br>
> <br>
</blockquote></div></div>