[英]HashSet “cannot convert from element type Object to Integer” when expecting a HashSet of Integer
[英]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);
}
}
我的预期输出是true 。
但实际输出是假的。
我的理解是在整数数据类型之间将分配最大的类型。 如果是这样,这叫什么?
true ? 1 : 2l
的 then 和 else 部分true ? 1 : 2l
true ? 1 : 2l
是int
和long
。 结果是最宽的, long
,然后部分被转换为长。 请参阅JLS 。
在计算机科学中,一个术语是平衡类型。
34 / 2.0 // double, more a case of _widening a type_.
c ? 2.0 : 34 // double
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.