面試時經常會問起字符串比較相關的問題,比如:字符串比較時用的什麼方法,內部實現如何?hashcode的作用,以及重寫equal方法,為什麼要重寫hashcode方法?以下就為大家解答,需要的朋友可以參考下
PS:本文使用jdk1.7
解析
1.Object類 的equals 方法
復制代碼 代碼如下:
/**
* Indicates whether some other object is "equal to" this one.
* <p>
* The {@code equals} method implements an equivalence relation
* on non-null object references:
* <ul>
* <li>It is <i>reflexive</i>: for any non-null reference value
* {@code x}, {@code x.equals(x)} should return
* {@code true}.
* <li>It is <i>symmetric</i>: for any non-null reference values
* {@code x} and {@code y}, {@code x.equals(y)}
* should return {@code true} if and only if
* {@code y.equals(x)} returns {@code true}.
* <li>It is <i>transitive</i>: for any non-null reference values
* {@code x}, {@code y}, and {@code z}, if
* {@code x.equals(y)} returns {@code true} and
* {@code y.equals(z)} returns {@code true}, then
* {@code x.equals(z)} should return {@code true}.
* <li>It is <i>consistent</i>: for any non-null reference values
* {@code x} and {@code y}, multiple invocations of
* {@code x.equals(y)} consistently return {@code true}
* or consistently return {@code false}, provided no
* information used in {@code equals} comparisons on the
* objects is modified.
* <li>For any non-null reference value {@code x},
* {@code x.equals(null)} should return {@code false}.
* </ul>
* <p>
* The {@code equals} method for class {@code Object} implements
* the most discriminating possible equivalence relation on objects;
* that is, for any non-null reference values {@code x} and
* {@code y}, this method returns {@code true} if and only
* if {@code x} and {@code y} refer to the same object
* ({@code x == y} has the value {@code true}).
* <p>
* Note that it is generally necessary to override the {@code hashCode}
* method whenever this method is overridden, so as to maintain the
* general contract for the {@code hashCode} method, which states
* that equal objects must have equal hash codes.
*
* @param obj the reference object with which to compare.
* @return {@code true} if this object is the same as the obj
* argument; {@code false} otherwise.
* @see #hashCode()
* @see java.util.HashMap
*/
public boolean equals(Object obj) {
return (this == obj);
}
看代碼,Object的equals方法,采用== 進行比較,只是比較對象的引用,如果引用的對象相同,那麼就返回true.
看注釋,Object的equals方法,具有如下特性
1.reflexive-自反性
x.equals(x) return true
2.symmetric-對稱性
x.equals(y) return true
y.equals(x) return true
3.transitive-傳遞性
x.equals(y) return true
y.equals(z) return true
x.equals(z) return true
4.consistent-一致性
x.equals(y) return true //那麼不管調用多少次,肯定都是返回true
5.與null的比較
x.equals(null) return false //對於none-null的x對象,每次必然返回false
6.於hashcode的關系
* Note that it is generally necessary to override the {@code hashCode}
* method whenever this method is overridden, so as to maintain the
* general contract for the {@code hashCode} method, which states
* that equal objects must have equal hash codes.
需要注意的是,一般來說,如果重寫了equals方法,都必須要重寫hashcode方法,
來確保具有相同引用的對象,能夠具有同樣的hashcode值
好了,看到這裡,我們就明白了,為什麼重寫了equals方法,一般來說就需要重寫hashcode方法,
雖然這個不是強制性的,但是如果不能保證相同的引用對象,沒有相同的hashcode,會對系統留下很大隱患
2.String類的equals方法
復制代碼 代碼如下:
/**
* Compares this string to the specified object. The result is {@code
* true} if and only if the argument is not {@code null} and is a {@code
* String} object that represents the same sequence of characters as this
* object.
*
* @param anObject
* The object to compare this {@code String} against
*
* @return {@code true} if the given object represents a {@code String}
* equivalent to this string, {@code false} otherwise
*
* @see #compareTo(String)
* @see #equalsIgnoreCase(String)
*/
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