java和equals区别_JAVA中==与equals的区别
equals如果沒有被重寫的話,和==的作用是一樣的,都是判斷兩個對象引用是否指向同一個地址。一般重寫了equals()方法就表示比較它們“實際意義上相等”,比較的是內容,而不是引用地址。Java中String重寫了equals方法,所以此時比較的是兩者的內容是否相等,而==比較的永遠是地址。
packagetestPage;/** ==和equals有何區別
* 這是在網上偶然看到的一個帖子,如果讓我說,還真說不清楚,所以決定一探究竟。
**/
public classStringAndEquals {public static voidmain(String[] args) {//案例1
String a = "abc";
String b= "abc";
System.out.println("a==b的結果是:" + (a == b)); //結果:true
System.out.println("a.equals(b)的結果是" + (a.equals(b))); //結果:true//案例2
String c = new String("abc");
String d= new String("abc");
System.out.println("c==d的結果是:" + (c == d)); //結果:false
System.out.println("c.equals(d)的結果是" + (c.equals(d))); //結果:true//案例3
String s1 = "Monday";
String s2= new String("Monday");if (s1 ==s2) {
System.out.println("s1 == s2");
}else{
System.out.println("s1 != s2"); //輸出這句話
}if(s1.equals(s2)) {
System.out.println("s1 equals s2"); //輸出這句話
} else{
System.out.println("s1 not equals s2");
}//案例4
String s3 = "Monday";
String s4= new String("Monday");
s4=s4.intern();if (s3 ==s4) {
System.out.println("s3 == s4"); //輸出這句話
} else{
System.out.println("s3 != s4");
}if(s1.equals(s4)) {
System.out.println("s3 equals s4"); //輸出這句話
} else{
System.out.println("s3 not equals s4");
}
}
}
對于案例1,在內存堆中還有個東西就“串池”,當你以賦值的形式,即String?a?=?"abc";給a賦值,系統會先在“串池”里找有沒有,沒有就創建,如果有,就直接給賦值了。在這里例子中String?a?=?"abc",之前“串池”中沒有,于是系統創建了一個,String?b?=?"abc"到“串池”中尋找的時候,有了,于是直接賦與內存地址,因此輸出為true。
對于案例2,直接new出來的字符串不在“串池”中,因此每次new的是不“==”的。
對于案例3,結合前兩個案例的解析,很容易開出原因。
對于案例4,加入了inter()方法,intern()后字符串可以直接 == 進行比較,速度提高了3倍。java.lang.String的intern()方法"abc".intern()方法的返回值還是字符串"abc",表面上看起來好像這個方法沒什么用處。但實際上,它做了個小動作:檢查字符串池里是否存在"abc"這么一個字符串,如果存在,就返回池里的字符串;如果不存在,該方法會 把"abc"添加到字符串池中,然后再返回它的引用。
我們清楚了用法之后,看一下原理吧,首先是equals的源碼:
public booleanequals(Object anObject) {if (this ==anObject) {return true;
}
}
如果重寫了equals方法,比如String類中的equals方法,源碼如下:
/*** Compares this string to the specified object. The result is {@code* true} if and only if the argument is not {@codenull} and is a {@code* String} object that represents the same sequence of characters as this
* object.
*
*@paramanObject
* The object to compare this {@codeString} against
*
*@return{@codetrue} if the given object represents a {@codeString}
* equivalent to this string, {@codefalse} otherwise
*
*@see#compareTo(String)
*@see#equalsIgnoreCase(String)*/
public booleanequals(Object anObject) {if (this ==anObject) {return true;
}if (anObject instanceofString) {
String anotherString=(String)anObject;int n =count; // 這里的count是事先定義的一個屬性,初始值為字符串的sizeif (n ==anotherString.count) {char v1[] =value; // value為字符串轉化的字符數組char v2[] =anotherString.value;int i =offset; // offset默認為0int j =anotherString.offset;while (n-- != 0) {if (v1[i++] != v2[j++])return false;
}return true;
}
}return false;
}
從上面的代碼,我們可以這樣其實在地址不相等的情況下會繼續比較值,如果值相等,返回true。而==返回的結果和重不重寫equals沒關系,重寫equals只會對Object.equals(Object)的結果造成影響。
所以,總的來說,我們可以得到下面的結論:
java中的數據類型,可分為兩類:
1.基本數據類型,也稱原始數據類型。byte,short,char,int,long,float,double,boolean他們之間的比較,應用雙等號(==),比較的是他們的值。
2.復合數據類型(類)
當他們用(==)進行比較的時候,比較的是他們在內存中的存放地址,所以,除非是同一個new出來的對象,他們的比較后的結果為true,否則比較后結果為false。?JAVA當中所有的類都是繼承于Object這個基類的,在Object中的基類中定義了一個equals的方法,這個方法的初始行為是比較對象的內存地址,但在一些類庫當中這個方法被覆蓋掉了,如String,Integer,Date在這些類當中equals有其自身的實現,而不再是比較類在堆內存中的存放地址了。
參考資料:
http://bbs.csdn.net/topics/300179403
http://www.cnblogs.com/zhxhdean/archive/2011/03/25/1995431.html
http://geyubin.iteye.com/blog/1145464
總結
以上是生活随笔為你收集整理的java和equals区别_JAVA中==与equals的区别的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java gridbag_java –
- 下一篇: java io操作_Java IO 操作