简体   繁体   中英

Integer becomes Long when passed to the method that is expecting a Object Type

public class Main
{
    public static void main(String args[])
    {
        process(true ? 1 : 2L);
    }


    static void process(Object object)
    {
        System.out.println(object instanceof Integer);
    }
}

My expected output is true .

But actual out put is false .

My understanding is between integral data types the largest type will be assigned. If so what is this called?

The then and else parts of true ? 1 : 2l true ? 1 : 2l are int and long . The result is the widest, long , the the then-part is cast to long. See JLS .

In Computer Science a term for this is balancing types .

34 / 2.0        // double, more a case of _widening a type_.
c ? 2.0 : 34    // double

像指出这里,具有三元表达式intlong将经受二进制数字转换,从而导致long

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM