tertiary operator error casting Float to Object
Liangtao
gliangtao at gmail.com
Sat Sep 16 02:00:14 UTC 2023
OpenJDK 17.0.8.1 also gets "60.0".
So it's actually "right" according to JLS. However, as a conservative guy I
lose confidence of using tertiary operator now...
Thanks,
Liangtao
On Fri, Sep 15, 2023 at 6:51 PM Alex Buckley <alex.buckley at oracle.com>
wrote:
> 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
> >
> >
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/compiler-dev/attachments/20230915/87246096/attachment-0001.htm>
More information about the compiler-dev
mailing list