String物件的equals()與 = =
最近看了網上有人說,String物件的equals()與==的區別,不知道兩者到底有什麼區別,如果真的要徹底搞清楚,我們還是從“萬類之源”說起,http://blog.itpub.net/29876893/viewspace-1819489/這是之前寫關於Object的一篇文章,現在我們還是從原始碼入手(當然結合API更好)。
下面還是分析Object類中的equals()方法,關於原始碼如何獲取上篇文章已經說的很清楚。
下面是Object類中的equals():
從上面我們可以看出來,比較的是兩個物件的地址值,更全面的介紹,自己看下上面的 註釋。
下面看下toString()方法,當然和本篇沒有多少關係。
返回的是包名.類名@十六進位制的雜湊值
下面看一小程式:
上面結果也許你會問,不都是字串麼!為什麼結果不一樣呢!?這個要結合java記憶體來分析,對於new出來的物件,是在堆中分配記憶體空間,而對於String類很特殊,
如果對於直接String ss = xxxx,是在常量池中分配記憶體空間,並不是在棧中分配。但是對於上述的a,b在棧中分配空間,指向了堆中不同的記憶體空間,當然兩者不等!
再看一個小程式:
咦,equals()不是比較的是地址麼?還是true,不能有這種定向思維,應該查閱API或者看下原始碼:
上面很清楚,String類重寫了Object中的toString()方法,如果不是指向同一物件的話,那就比較內容是不是一樣,所以上文中a.equals(b),b.equals(a)返回的是true.
在繼承父類時,重寫某個方法很重要。上文中沒有說Object類中一個也很重要的方法hashcode(),這也是後續文章會寫。文章作為本人學習總結,如有錯誤之處,請指正!謝謝。
下面還是分析Object類中的equals()方法,關於原始碼如何獲取上篇文章已經說的很清楚。
下面是Object類中的equals():
點選(此處)摺疊或開啟
-
public boolean equals(Object obj) {
-
return (this == obj);
-
}
-
-
-
/**
-
* Creates and returns a copy of this object. The precise meaning
-
* of "copy" may depend on the class of the object. The general
-
* intent is that, for any object {@code x}, the expression:
-
*
-
*
-
* x.clone() != x
-
* will be true, and that the expression:
-
*
-
*
-
* x.clone().getClass() == x.getClass()
-
* will be {@code true}, but these are not absolute requirements.
-
* While it is typically the case that:
-
*
-
*
-
* x.clone().equals(x)
-
* will be {@code true}, this is not an absolute requirement.
-
*
-
* By convention, the returned object should be obtained by calling
-
* {@code super.clone}. If a class and all of its superclasses (except
-
* {@code Object}) obey this convention, it will be the case that
-
* {@code x.clone().getClass() == x.getClass()}.
-
*
-
* By convention, the object returned by this method should be independent
-
* of this object (which is being cloned). To achieve this independence,
-
* it may be necessary to modify one or more fields of the object returned
-
* by {@code super.clone} before returning it. Typically, this means
-
* copying any mutable objects that comprise the internal "deep structure"
-
* of the object being cloned and replacing the references to these
-
* objects with references to the copies. If a class contains only
-
* primitive fields or references to immutable objects, then it is usually
-
* the case that no fields in the object returned by {@code super.clone}
-
* need to be modified.
-
*
-
* The method {@code clone} for class {@code Object} performs a
-
* specific cloning operation. First, if the class of this object does
-
* not implement the interface {@code Cloneable}, then a
-
* {@code CloneNotSupportedException} is thrown. Note that all arrays
-
* are considered to implement the interface {@code Cloneable} and that
-
* the return type of the {@code clone} method of an array type {@code T[]}
-
* is {@code T[]} where T is any reference or primitive type.
-
* Otherwise, this method creates a new instance of the class of this
-
* object and initializes all its fields with exactly the contents of
-
* the corresponding fields of this object, as if by assignment; the
-
* contents of the fields are not themselves cloned. Thus, this method
-
* performs a "shallow copy" of this object, not a "deep copy" operation.
-
*
-
* The class {@code Object} does not itself implement the interface
-
* {@code Cloneable}, so calling the {@code clone} method on an object
-
* whose class is {@code Object} will result in throwing an
-
* exception at run time.
-
*
-
* @return a clone of this instance.
-
* @exception CloneNotSupportedException if the object's class does not
-
* support the {@code Cloneable} interface. Subclasses
-
* that override the {@code clone} method can also
-
* throw this exception to indicate that an instance cannot
-
* be cloned.
-
* @see java.lang.Cloneable
-
*/
下面看下toString()方法,當然和本篇沒有多少關係。
點選(此處)摺疊或開啟
-
public String toString() {
-
return getClass().getName() + "@" + Integer.toHexString(hashCode());
-
}
-
下面看一小程式:
上面結果也許你會問,不都是字串麼!為什麼結果不一樣呢!?這個要結合java記憶體來分析,對於new出來的物件,是在堆中分配記憶體空間,而對於String類很特殊,
如果對於直接String ss = xxxx,是在常量池中分配記憶體空間,並不是在棧中分配。但是對於上述的a,b在棧中分配空間,指向了堆中不同的記憶體空間,當然兩者不等!
再看一個小程式:
咦,equals()不是比較的是地址麼?還是true,不能有這種定向思維,應該查閱API或者看下原始碼:
點選(此處)摺疊或開啟
-
*/
-
public boolean equals(Object anObject) {
-
if (this == anObject) {
-
return true;
-
}
-
if (anObject instanceof String) {
-
String anotherString = (String) anObject;
-
int n = value.length;
-
if (n == anotherString.value.length) {
-
char v1[] = value;
-
char v2[] = anotherString.value;
-
int i = 0;
-
while (n-- != 0) {
-
if (v1[i] != v2[i])
-
return false;
-
i++;
-
}
-
return true;
-
}
-
}
-
return false;
- }
在繼承父類時,重寫某個方法很重要。上文中沒有說Object類中一個也很重要的方法hashcode(),這也是後續文章會寫。文章作為本人學習總結,如有錯誤之處,請指正!謝謝。
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29876893/viewspace-1823819/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- String類中的equals方法與Object類中的equals方法的不同點Object
- Java之String的equals與contentEquals區別Java
- java String的equals,intern方法Java
- String的equals和hashCode方法
- Object類和String類equals方法的區別Object
- Redis的String物件Redis物件
- hashCode()與equals()
- Java物件之間的比較之equals和==Java物件
- Java中(==)與equals的區別Java
- equals與==的區別(詳解)
- C#之Equals與==C#
- Java面試題 equals()與"=="的區別?Java面試題
- Javascript String物件方法JavaScript物件
- JavaScript 基礎(二) – 建立 function 物件的方法, String物件, Array物件JavaScriptFunction物件
- 面試官愛問的equals與hashCode面試
- 常見物件-String類物件
- 物件只定義了Equals和Hashcode方法之一的漏洞物件
- 關於建立String物件的抉擇物件
- Array String物件的方法和屬性物件
- String a = "abc" 與String b = new String("abc")的區別
- 讓你徹底理解 “==”與 Equals
- equals與hashCode關係梳理
- 將一個物件多次放入set不呼叫equals方法物件
- String s = new String(" a ") 到底產生幾個物件?物件
- String s=new String("abc")建立了幾個物件?物件
- Java基礎-- ==號與equals()方法的區別Java
- javaSE中的==和equals的聯絡與區別Java
- DDD實體值物件的equals和hashcode方法實現 - wimdeblauwe物件
- 物件包裝器類之間的比較用equals()而不是==物件
- java字串“==”與“equals”的差異及與c#的區別Java字串C#
- JavaScript 複習之 String 物件JavaScript物件
- 建立了幾個String物件?物件
- 常見物件-String類-2物件
- 常見物件-String類-3物件
- 常見物件-String類-4物件
- string的find()與npos
- 支援正規表示式的 String 物件的方法物件
- HashCode 和 Equals 的使用 - 使用自定義物件作為HashMap的Key例子物件HashMap