[英]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.