簡體   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);
    }
}

我的預期輸出是true

但實際輸出是假的

我的理解是在整數數據類型之間將分配最大的類型。 如果是這樣,這叫什么?

true ? 1 : 2l的 then 和 else 部分true ? 1 : 2l true ? 1 : 2lintlong 結果是最寬的, long ,然后部分被轉換為長。 請參閱JLS

在計算機科學中,一個術語是平衡類型

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM