tertiary operator error casting Float to Object

Liangtao gliangtao at gmail.com
Fri Sep 15 23:21:50 UTC 2023


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/61fb3274/attachment.htm>


More information about the compiler-dev mailing list