[英]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 : 2l
是int
和long
。 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.