Java Integer.class VS Integer.TYPE VS int.class

更多 Java 基礎(chǔ)知識方面的文章,請參見文集《Java 基礎(chǔ)知識》


Integer.class VS int.class

相同點:都會得到 Class<Integer>

不同點:

  • Integer 是 Object Type 對象類型,int 是 Primitive Type 原始類型
  • Integer 有成員變量,有成員方法,int 無成員變量,無成員方法
  • Integer:a reference to an int primitive
  • int:a literal numerical value

示例:

public static void main(String[] args) {
    Integer i1 = 123;
    int i2 = 123;

    Class<Integer> c1 = Integer.class;
    Class<Integer> c2 = int.class;

    // False
    System.out.println(c1 == c2);
    // False
    System.out.println(c1.isPrimitive());
    // True
    System.out.println(c2.isPrimitive());
}

Integer.TYPE

得到 Class<Integer>:The class instance representing the primitive type int。
因此:

  • Integer.classint.class 不同
  • Integer.TYPEint.class 相同

示例:

public static void main(String[] args) {
    Integer i1 = 123;
    int i2 = 123;

    Class<Integer> c1 = Integer.TYPE;
    Class<Integer> c2 = int.class;

    // True
    System.out.println(c1 == c2);
    // True
    System.out.println(c1.isPrimitive());
    // True
    System.out.println(c2.isPrimitive());
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,551評論 19 139
  • 對象的創(chuàng)建與銷毀 Item 1: 使用static工廠方法,而不是構(gòu)造函數(shù)創(chuàng)建對象:僅僅是創(chuàng)建對象的方法,并非Fa...
    孫小磊閱讀 2,183評論 0 3
  • "Unterminated string literal.": "未終止的字符串文本。", "Identifier...
    兩個心閱讀 8,557評論 0 4
  • 太陽贏 雙方目前都處于擔心與憂慮當中,對賽事,對手充滿了不確定~ 太陽隊相對更加穩(wěn)定與扎實寫好,穩(wěn)扎穩(wěn)打,進球次數(shù)...
    意守兩眉閱讀 356評論 0 0
  • 有這樣一句“慢性病不可怕,可怕的是慢性病綜合癥”。我家人的糖尿病,其實不是很高,但是由于錯過了最佳的治療期,出...
    健康顧問飛雁閱讀 725評論 0 0

友情鏈接更多精彩內(nèi)容