淺談Java中Int、Integer、Integer.valueOf()、new Integer()之間的區(qū)別
Int
Int是Java八種基本數(shù)據(jù)類型之一,一般大小為4字節(jié)32位,取值范圍為2-31—231。兩個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)會自動將這個Int值封裝成一個Integer對象。
比如:Integer a = 100;實際上的操作是:Integer a = Integer.valueOf(100);
以下是valueOf()的源碼

注意:這里Integer.valueOf(),當Int值的范圍在-128-127之間時,會通過一個IntegerCache緩存來創(chuàng)建Integer對象;當Int值不在該范圍時,直接調用new Integer()來創(chuàng)建對象,因此會出現(xiàn)以下的情況
(1)Integer a = 100; Integer b = 100; a==b結果為true,因為這兩個Integer變量引用的是緩存中的同一個Integer對象 ;
(2)Integer c = 200; Integer d = 200; a==b結果為false,因為a和b是通過new Integer() 創(chuàng)建的兩個不同對象。
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()每次都會創(chuàng)建新的對象,==比較的是兩個對象的內存地址
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對象,還是通過直接賦值Int值創(chuàng)建的Integer對象,它們與Int類型變量通過“==”進行比較時都會自動拆箱變成Int類型,所以Integer對象和Int變量比較的是內容大小。
package com.company.algorithm;
public class Main {
public static void main(String[] args) {
int a = 100;
Integer b = 100;//等價于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對象和直接賦Int值創(chuàng)建的Integer對象使用==比較的是它們的內存地址。
package com.company.algorithm;
public class Main {
public static void main(String[] args) {
Integer b = 100;//等價于b=Integer.valueOf(100);
Integer c = new Integer(100);
System.out.println(b == c);//false
}
}
(3)賦Int值創(chuàng)建的Integer對象比較:
當Int值在-128-127之間時,兩個Integer變量引用的是IntegerCache中的同一個對象,內存地址相同,因此==的結果為true;
當Int值不在以上范圍時,兩個Integer對象都是通過new創(chuàng)建的,內存地址不同,因此==的結果為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
}
}
一個金典面試題
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
}
}
到此這篇關于淺談Java中Int、Integer、Integer.valueOf()、new Integer()之間的區(qū)別的文章就介紹到這了,更多相關Java Int Integer Integer.valueOf() newInteger內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
springBoot 打war包 程序包com.sun.istack.internal不存在的問題及解決方案
這篇文章主要介紹了springBoot 打war包 程序包com.sun.istack.internal不存在的問題及解決方案,親測試過可以,需要的朋友可以參考下2018-07-07
使用maven實現(xiàn)redis與idea的連接問題
這篇文章主要介紹了使用maven實現(xiàn)redis與idea的連接問題,本文通過示例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-07-07
利用Spring Cloud Config結合Bus實現(xiàn)分布式配置中心的步驟
這篇文章主要介紹了利用Spring Cloud Config結合Bus實現(xiàn)分布式配置中心的相關資料,文中通過示例代碼將實現(xiàn)的步驟一步步介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友下面來一起看看吧2018-05-05
解決IDEA2020.1.2IDEA打不開的問題(最新分享)
由于idea安裝多了某個jar,點擊出現(xiàn)讀條后閃退情況,接下來通過本文給大家分享解決IDEA2020.1.2IDEA打不開的問題,非常不錯,具有一定的參考借鑒價值,感興趣的朋友跟隨小編一起看看吧2020-07-07

