淺談Java中Int、Integer、Integer.valueOf()、new Integer()之間的區(qū)別
Int
Int是Java八種基本數(shù)據(jù)類型之一,一般大小為4字節(jié)32位,取值范圍為2-31—231。兩個(gè)Int類型變量用“==”比較的是值的大小。
package com.company.algorithm;
public class Main {
public static void main(String[] args) {
int a = 100;
int b = 100;
System.out.println(a == b);//true
}
}
Integer和Integer.valueOf()
將Int值賦給Integer變量,系統(tǒng)會(huì)自動(dòng)將這個(gè)Int值封裝成一個(gè)Integer對(duì)象。
比如:Integer a = 100;實(shí)際上的操作是:Integer a = Integer.valueOf(100);
以下是valueOf()的源碼

注意:這里Integer.valueOf(),當(dāng)Int值的范圍在-128-127之間時(shí),會(huì)通過一個(gè)IntegerCache緩存來創(chuàng)建Integer對(duì)象;當(dāng)Int值不在該范圍時(shí),直接調(diào)用new Integer()來創(chuàng)建對(duì)象,因此會(huì)出現(xiàn)以下的情況
(1)Integer a = 100; Integer b = 100; a==b結(jié)果為true,因?yàn)檫@兩個(gè)Integer變量引用的是緩存中的同一個(gè)Integer對(duì)象 ;
(2)Integer c = 200; Integer d = 200; a==b結(jié)果為false,因?yàn)閍和b是通過new Integer() 創(chuàng)建的兩個(gè)不同對(duì)象。
package com.company.algorithm;
public class Main {
public static void main(String[] args) {
Integer a = 100;
Integer b = 100;
Integer c = 200;
Integer d = 200;
System.out.println(a == b);//true
System.out.println(c == d);//false
}
}
new Integer()
new Integer()每次都會(huì)創(chuàng)建新的對(duì)象,==比較的是兩個(gè)對(duì)象的內(nèi)存地址
package com.company.algorithm;
public class Main {
public static void main(String[] args) {
Integer a = new Integer(100);
Integer b = new Integer(100);
System.out.println(a == b);//false
}
}
三者之間的比較
(1)不管是new創(chuàng)建的Integer對(duì)象,還是通過直接賦值Int值創(chuàng)建的Integer對(duì)象,它們與Int類型變量通過“==”進(jìn)行比較時(shí)都會(huì)自動(dòng)拆箱變成Int類型,所以Integer對(duì)象和Int變量比較的是內(nèi)容大小。
package com.company.algorithm;
public class Main {
public static void main(String[] args) {
int a = 100;
Integer b = 100;//等價(jià)于b=Integer.valueOf(100);
Integer c = new Integer(100);
System.out.println(a == b);//true
System.out.println(a == c);//true
}
}
(2)new創(chuàng)建的Integer對(duì)象和直接賦Int值創(chuàng)建的Integer對(duì)象使用==比較的是它們的內(nèi)存地址。
package com.company.algorithm;
public class Main {
public static void main(String[] args) {
Integer b = 100;//等價(jià)于b=Integer.valueOf(100);
Integer c = new Integer(100);
System.out.println(b == c);//false
}
}
(3)賦Int值創(chuàng)建的Integer對(duì)象比較:
當(dāng)Int值在-128-127之間時(shí),兩個(gè)Integer變量引用的是IntegerCache中的同一個(gè)對(duì)象,內(nèi)存地址相同,因此==的結(jié)果為true;
當(dāng)Int值不在以上范圍時(shí),兩個(gè)Integer對(duì)象都是通過new創(chuàng)建的,內(nèi)存地址不同,因此==的結(jié)果為false
package com.company.algorithm;
public class Main {
public static void main(String[] args) {
Integer a = 100;
Integer b = 100;
Integer c = 200;
Integer d = 200;
Integer f = new Integer(100);
System.out.println(a == b);//true
System.out.println(c == d);//false
System.out.println(a == f);//false
}
}
一個(gè)金典面試題
package com.company.algorithm;
public class Main {
public static void main(String[] args) {
Integer a = 49;
int b = 49;
Integer c = Integer.valueOf(49);
Integer d = new Integer(49);
System.out.println(a == b);//true
System.out.println(a == c);//true
System.out.println(b == c);//true
System.out.println(c == d);//false
}
}
到此這篇關(guān)于淺談Java中Int、Integer、Integer.valueOf()、new Integer()之間的區(qū)別的文章就介紹到這了,更多相關(guān)Java Int Integer Integer.valueOf() newInteger內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Java 如何判斷Integer類型的值是否相等
- Java Integer對(duì)象的比較方式
- Java修改Integer變量值遇到的問題及解決
- JAVA基本類型包裝類 BigDecimal BigInteger 的使用
- Java 處理超大數(shù)類型之BigInteger案例詳解
- 詳談java中int和Integer的區(qū)別及自動(dòng)裝箱和自動(dòng)拆箱
- 聊聊Java BigInteger里面的mod和remainder的區(qū)別
- 你知道在Java中Integer和int的這些區(qū)別嗎?
- 詳解Java中包裝類Integer的使用
- Java中int和Integer的區(qū)別
相關(guān)文章
springBoot 打war包 程序包c(diǎn)om.sun.istack.internal不存在的問題及解決方案
這篇文章主要介紹了springBoot 打war包 程序包c(diǎn)om.sun.istack.internal不存在的問題及解決方案,親測(cè)試過可以,需要的朋友可以參考下2018-07-07
java web實(shí)現(xiàn)簡(jiǎn)單留言板功能
這篇文章主要為大家詳細(xì)介紹了java web實(shí)現(xiàn)簡(jiǎn)單留言板功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-11-11
使用maven實(shí)現(xiàn)redis與idea的連接問題
這篇文章主要介紹了使用maven實(shí)現(xiàn)redis與idea的連接問題,本文通過示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-07-07
利用Spring Cloud Config結(jié)合Bus實(shí)現(xiàn)分布式配置中心的步驟
這篇文章主要介紹了利用Spring Cloud Config結(jié)合Bus實(shí)現(xiàn)分布式配置中心的相關(guān)資料,文中通過示例代碼將實(shí)現(xiàn)的步驟一步步介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友下面來一起看看吧2018-05-05
解決IDEA2020.1.2IDEA打不開的問題(最新分享)
由于idea安裝多了某個(gè)jar,點(diǎn)擊出現(xiàn)讀條后閃退情況,接下來通過本文給大家分享解決IDEA2020.1.2IDEA打不開的問題,非常不錯(cuò),具有一定的參考借鑒價(jià)值,感興趣的朋友跟隨小編一起看看吧2020-07-07
Java實(shí)現(xiàn)簡(jiǎn)單密碼加密功能
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)簡(jiǎn)單密碼加密功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-03-03

