代碼詳解java里的“==”和“equels”區(qū)別
測(cè)試1:
先看一組String類(lèi)型比較,廢話不多說(shuō),直接上代碼:
public class Test {
public static void main(String[] args) {
String a = "java書(shū)苑";
String b = "java書(shū)苑";
String c = new String("java書(shū)苑");
String d = new String("java書(shū)苑").intern();
if(a == b){
System.out.println("a == b");
}else{
System.out.println("a != b");
}
if(a.equals(b)){
System.out.println("a.equals(b)");
}else{
System.out.println("!a.equals(b)");
}
if(a == c){
System.out.println("a == c");
}else{
System.out.println("a != c");
}
if(a.equals(c)){
System.out.println("a.equals(c)");
}else{
System.out.println("!a.equals(c)");
}
if(a == d){
System.out.println("a == d");
}else{
System.out.println("a != d");
}
if(a.equals(d)){
System.out.println("a.equals(d)");
}else{
System.out.println("a.equals(d)");
}
}
}
輸出結(jié)果:
a == b a.equals(b) a != c a.equals(c) a == d a.equals(d)
總結(jié):
結(jié)果a == b:程序在運(yùn)行的時(shí)候會(huì)創(chuàng)建一個(gè)字符串緩沖池,在String a = “java書(shū)苑”時(shí), “java書(shū)苑”被放到了字符串緩沖池中,當(dāng) String b = “java書(shū)苑” 創(chuàng)建字符串的時(shí)候,程序首先會(huì)在這個(gè)String緩沖池中尋找相同值的對(duì)象,所以在b被創(chuàng)建的時(shí)候,程序找到了具有相同值的a,將b 引用 a 所引用的對(duì)象。所以a和b引用的同一個(gè)對(duì)象,故a == b。
結(jié)果a != c:String c = new String(“java書(shū)苑”)時(shí)new了一個(gè)新的對(duì)象,故不從String緩沖池尋找,二十直接創(chuàng)建一個(gè)新的對(duì)象。所以a != c。
結(jié)果a == d :當(dāng)調(diào)用 intern 方法時(shí),如果池已經(jīng)包含一個(gè)等于此 String 對(duì)象的字符串(該對(duì)象由 equals(Object) 方法確定),則返回池中的字符串。否則,將此 String 對(duì)象添加到池中,并且返回此 String 對(duì)象的引用。所有d調(diào)用的同樣是a的對(duì)象。
equals比較的是值,故值一樣時(shí)便相等。
測(cè)試2:
這是一組int類(lèi)型和Integer類(lèi)型的測(cè)試:
public class Test {
public static void main(String[] args) {
int a = 127;
int a1 = 127;
int b = 128;
int b1 = 128;
Integer c = 127;
Integer c1 = 127;
Integer d = 128;
Integer d1 = 128;
if(a == a1){
System.out.println("a == a1");
}else{
System.out.println("a != a1");
}
if(b == b1){
System.out.println("b == b1");
}else{
System.out.println("b != b1");
}
if(c == c1){
System.out.println("c == c1");
}else{
System.out.println("c != c1");
}
if(d == d1){
System.out.println("d == d1");
}else{
System.out.println("d != d1");
}
}
}
輸出的結(jié)果:
a == a1 b == b1 c == c1 d != d1
結(jié)果”a == a1”和”b == b1”:int 是基本類(lèi)型,直接存數(shù)值,而integer是對(duì)象,用一個(gè)引用指向這個(gè)對(duì)象,多以比較的時(shí)候”a == a1”和”b == b1”。
結(jié)果“c == c1”和“d != d1”這里可能有人會(huì)有疑問(wèn),為什么“d != d1”.我們一起看一下Integer的源碼。
/**
* Cache to support the object identity semantics of autoboxing for values between
* -128 and 127 (inclusive) as required by JLS.
*
* The cache is initialized on first usage. The size of the cache
* may be controlled by the -XX:AutoBoxCacheMax=<size> option.
* During VM initialization, java.lang.Integer.IntegerCache.high property
* may be set and saved in the private system properties in the
* sun.misc.VM class.
*/
private static class IntegerCache {
static final int low = -128;
static final int high;
static final Integer cache[];
static {
// high value may be configured by property
int h = 127;
String integerCacheHighPropValue =
sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
if (integerCacheHighPropValue != null) {
int i = parseInt(integerCacheHighPropValue);
i = Math.max(i, 127);
// Maximum array size is Integer.MAX_VALUE
h = Math.min(i, Integer.MAX_VALUE - (-low) -1);
}
high = h;
cache = new Integer[(high - low) + 1];
int j = low;
for(int k = 0; k < cache.length; k++)
cache[k] = new Integer(j++);
}
private IntegerCache() {}
}
/**
* Returns an {@code Integer} instance representing the specified
* {@code int} value. If a new {@code Integer} instance is not
* required, this method should generally be used in preference to
* the constructor {@link #Integer(int)}, as this method is likely
* to yield significantly better space and time performance by
* caching frequently requested values.
*
* This method will always cache values in the range -128 to 127,
* inclusive, and may cache other values outside of this range.
*
* @param i an {@code int} value.
* @return an {@code Integer} instance representing {@code i}.
* @since 1.5
*/
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);
}
結(jié)論:這里 Integer 會(huì)初始化一個(gè)[-128,127]的常量池,如果數(shù)值在這個(gè)范圍時(shí),則引用的是同一個(gè)對(duì)象,如果不在這個(gè)范圍,通過(guò)源碼可以看出返回的是new了一個(gè)新的對(duì)象: return new Integer(i);
所以,結(jié)果“c == c1”是引用了同一個(gè)對(duì)象,結(jié)果“d != d1”,是new了一個(gè)新的對(duì)象,故不等。
相關(guān)文章
mybatis-plus雪花算法增強(qiáng)idworker的實(shí)現(xiàn)
今天聊聊在mybatis-plus中引入分布式ID生成框架idworker,進(jìn)一步增強(qiáng)實(shí)現(xiàn)生成分布式唯一ID,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-07-07
關(guān)于maven依賴 ${xxx.version}報(bào)錯(cuò)問(wèn)題
這篇文章主要介紹了關(guān)于maven依賴 ${xxx.version}報(bào)錯(cuò)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01
Easypoi 輕松實(shí)現(xiàn)復(fù)雜excel文件導(dǎo)出功能
這篇文章主要介紹了Easypoi 輕松實(shí)現(xiàn)復(fù)雜excel文件導(dǎo)出功能,本文通過(guò)示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-11-11
解決feign微服務(wù)間的文件上傳報(bào)錯(cuò)問(wèn)題
這篇文章主要介紹了解決feign微服務(wù)間的文件上傳報(bào)錯(cuò)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06
Java數(shù)組模擬優(yōu)先級(jí)隊(duì)列數(shù)據(jù)結(jié)構(gòu)的實(shí)例
這篇文章主要介紹了Java數(shù)組模擬優(yōu)先級(jí)隊(duì)列數(shù)據(jù)結(jié)構(gòu)的實(shí)例,優(yōu)先級(jí)隊(duì)列中的元素會(huì)被設(shè)置優(yōu)先權(quán),本文的例子借助了Java中的TreeSet和TreeMap,需要的朋友可以參考下2016-04-04

