Java數(shù)據(jù)類型Integer與int的區(qū)別詳細(xì)解析
Integer與int的區(qū)別
如果面試官問Integer與int的區(qū)別:估計(jì)大多數(shù)人只會(huì)說道兩點(diǎn),Ingeter是int的包裝類,int的初值為0,Ingeter的初值為null。
但是如果面試官再問一下Integer i = 1;int ii = 1; i==ii為true還是為false?
估計(jì)就有一部分人答不出來了,如果再問一下其他的,估計(jì)更多的人會(huì)頭腦一片混亂。
所以我對(duì)它們進(jìn)行了總結(jié),希望對(duì)大家有幫助。
package com.test;
/**
*
* @author 劉玲
*
*/
public class TestInteger {
/**
* @param args
*/
public static void main(String[] args) {
int i = 128;
Integer i2 = 128;
Integer i3 = new Integer(128);
//Integer會(huì)自動(dòng)拆箱為int,所以為true
System.out.println(i == i2);
System.out.println(i == i3);
System.out.println("**************");
Integer i5 = 127;//java在編譯的時(shí)候,被翻譯成-> Integer i5 = Integer.valueOf(127);
Integer i6 = 127;
System.out.println(i5 == i6);//true
/*Integer i5 = 128;
Integer i6 = 128;
System.out.println(i5 == i6);//false
*/ Integer ii5 = new Integer(127);
System.out.println(i5 == ii5); //false
Integer i7 = new Integer(128);
Integer i8 = new Integer(123);
System.out.println(i7 == i8); //false
}
}首先,17行和18行輸出結(jié)果都為true,因?yàn)镮nteger和int比都會(huì)自動(dòng)拆箱(jdk1.5以上)。
22行的結(jié)果為true,而25行則為false,很多人都不動(dòng)為什么。
其實(shí)java在編譯Integer i5 = 127的時(shí)候,被翻譯成-> Integer i5 = Integer.valueOf(127);
所以關(guān)鍵就是看valueOf()函數(shù)了。
只要看看valueOf()函數(shù)的源碼就會(huì)明白了。JDK源碼的valueOf函數(shù)式這樣的:
public static Integer valueOf(int i) {
assert IntegerCache.high >= 127;
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}看一下源碼大家都會(huì)明白,對(duì)于-128到127之間的數(shù),會(huì)進(jìn)行緩存,Integer i5 = 127時(shí),會(huì)將127進(jìn)行緩存,下次再寫Integer i6 = 127時(shí),就會(huì)直接從緩存中取,就不會(huì)new了。
所以22行的結(jié)果為true,而25行為false。
對(duì)于27行和30行,因?yàn)閷?duì)象不一樣,所以為false。
我對(duì)于以上的情況總結(jié)如下:
①無論如何,Integer與new Integer不會(huì)相等。不會(huì)經(jīng)歷拆箱過程,i3的引用指向堆,而i4指向?qū)iT存放他的內(nèi)存(常量池),他們的內(nèi)存地址不一樣,所以為false
②兩個(gè)都是非new出來的Integer,如果數(shù)在-128到127之間,則是true,否則為false。java在編譯Integer i2 = 128的時(shí)候,被翻譯成-> Integer i2 = Integer.valueOf(128);而valueOf()函數(shù)會(huì)對(duì)-128到127之間的數(shù)進(jìn)行緩存
③兩個(gè)都是new出來的,都為false
④int和integer(無論new否)比,都為true,因?yàn)闀?huì)把Integer自動(dòng)拆箱為int再去比。
到此這篇關(guān)于Java數(shù)據(jù)類型Integer與int的區(qū)別詳細(xì)解析的文章就介紹到這了,更多相關(guān)Integer與int的區(qū)別內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
springboot啟動(dòng)時(shí)沒有日志的原因分析
這篇文章主要介紹了springboot啟動(dòng)時(shí)沒有日志的原因分析,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07
Mybatis的association使用子查詢結(jié)果錯(cuò)誤的問題解決
本文主要介紹了Mybatis的association使用子查詢結(jié)果錯(cuò)誤的問題解決,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2025-07-07
解析整合mybatis-spring需要的maven依賴配置問題
這篇文章主要介紹了整合mybatis-spring需要的maven依賴配置問題,創(chuàng)建Maven項(xiàng)目,導(dǎo)入相關(guān)jar包,文中還給大家提到了,解決maven靜態(tài)資源約定大于習(xí)慣問題,本文給大家介紹的非常詳細(xì),需要的朋友參考下吧2021-11-11
JAVA中字符串函數(shù)subString的用法小結(jié)
本篇文章主要是對(duì)JAVA中字符串函數(shù)subString的用法進(jìn)行了詳細(xì)的介紹,需要的朋友可以過來參考下,希望對(duì)大家有所幫助2014-02-02
Struts2中validate數(shù)據(jù)校驗(yàn)的兩種方法詳解附Struts2常用校驗(yàn)器
這篇文章主要介紹了Struts2中validate數(shù)據(jù)校驗(yàn)的兩種方法及Struts2常用校驗(yàn)器,本文介紹的非常詳細(xì),具有參考借鑒價(jià)值,感興趣的朋友一起看看吧2016-09-09

