简体   繁体   English

传递给需要对象类型的方法时,整数变为长整数

[英]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 .我的预期输出是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的 then 和 else 部分true ? 1 : 2l true ? 1 : 2l are int and long . true ? 1 : 2lintlong The result is the widest, long , the the then-part is cast to long.结果是最宽的, long ,然后部分被转换为长。 See JLS .请参阅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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当期望HashSet为Integer时,HashSet“无法从元素类型Object转换为Integer” - HashSet “cannot convert from element type Object to Integer” when expecting a HashSet of Integer 当long值传递给get方法时,使用整数作为键的映射返回null - Map with integer as key returns null when long value is passed to the get method 将参数传递给void方法(整数或对象) - Passed parameter into void method (integer or object) 为什么foo(1,2,3)没有作为整数[]传递给varargs方法foo(Object ...) - Why is foo(1,2,3) not passed to varargs method foo(Object… ) as an Integer[] 泛型迭代器<e>当集合的原始类型 object 传递给接受通用参数的方法时,行为不同</e> - Generic Iterator<E> behaves differently when a Raw Type of Collection object is passed to method which accepts Generic parameter 接受长值的java方法是否应该接受Integer对象? - Should a java method accepting long value accept a Integer object? Bundle在传递给子活动时变为null - Bundle becomes null when passed to a sub activity 在Java中将负整数类型转换为long类型时会发生什么? - what happens when a negative integer is type cast to long in java? 当一个对象作为类型超类传入时,有没有办法检索子类的类型? - Is there a way of retrieving the type of a subclass, when an object is passed in as type super class? Java将传递给方法的对象转换为其原始类型 - Java casting an object passed to method to its original type
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM