== :比较内存地址
---------------------------------------------------
Object.equals():
public boolean equals(Object obj) {
return (this == obj);
}
Long.equals():
public boolean equals(Object obj) {
if (obj instanceof Long) {
return value == ((Long)obj).longValue();
}
return false;
}
Test:
Long a = new Long(1);
int b = 1;
System.out.println(a.equals(b));//false,b被装箱后比较a与b的内存地址
System.out.println(a==b);//true,a被拆箱后比较a与b的内存地址
Long a1 = new Long(1);
System.out.println(a == a1);//false
String.equals():
public boolean equals(Object anObject) {
if (this == anObject) {
return true;
}
HashMap.equals():
public final boolean equals(Object o) {
if (!(o instanceof Map.Entry))
return false;
Map.Entry e = (Map.Entry)o;
Object k1 = getKey();
Object k2 = e.getKey();
if (k1 == k2 || (k1 != null && k1.equals(k2))) {
Object v1 = getValue();
Object v2 = e.getValue();
if (v1 == v2 || (v1 != null && v1.equals(v2)))
return true;
}
return false;
}
---------------------------------------------------
Object.hashCode(): 返回内存地址
/**
* Returns a hash code value for the object. This method is
* supported for the benefit of hashtables such as those provided by
* <code>java.util.Hashtable</code>.
* <p>
* The general contract of <code>hashCode</code> is:
* <ul>
* <li>Whenever it is invoked on the same object more than once during
* an execution of a Java application, the <tt>hashCode</tt> method
* must consistently return the same integer, provided no information
* used in <tt>equals</tt> comparisons on the object is modified.
* This integer need not remain consistent from one execution of an
* application to another execution of the same application.
* <li>If two objects are equal according to the <tt>equals(Object)</tt>
* method, then calling the <code>hashCode</code> method on each of
* the two objects must produce the same integer result.
* <li>It is <em>not</em> required that if two objects are unequal
* according to the {@link java.lang.Object#equals(java.lang.Object)}
* method, then calling the <tt>hashCode</tt> method on each of the
* two objects must produce distinct integer results. However, the
* programmer should be aware that producing distinct integer results
* for unequal objects may improve the performance of hashtables.
* </ul>
* <p>
* As much as is reasonably practical, the hashCode method defined by
* class <tt>Object</tt> does return distinct integers for distinct
* objects. (This is typically implemented by converting the internal
* address of the object into an integer, but this implementation
* technique is not required by the
* Java<font size="-2"><sup>TM</sup></font> programming language.)
*
* @return a hash code value for this object.
* @see java.lang.Object#equals(java.lang.Object)
* @see java.util.Hashtable
*/
public native int hashCode();
Long.hashCode():返回Long的值
/**
* Returns a hash code for this <code>Long</code>. The result is
* the exclusive OR of the two halves of the primitive
* <code>long</code> value held by this <code>Long</code>
* object. That is, the hashcode is the value of the expression:
* <blockquote><pre>
* (int)(this.longValue()^(this.longValue()>>>32))
* </pre></blockquote>
*
* @return a hash code value for this object.
*/
public int hashCode() {
return (int)(value ^ (value >>> 32));
}
String.hashCode():
/**
* Returns a hash code for this string. The hash code for a
* <code>String</code> object is computed as
* <blockquote><pre>
* s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]
* </pre></blockquote>
* using <code>int</code> arithmetic, where <code>s[i]</code> is the
* <i>i</i>th character of the string, <code>n</code> is the length of
* the string, and <code>^</code> indicates exponentiation.
* (The hash value of the empty string is zero.)
*
* @return a hash code value for this object.
*/
public int hashCode() {
int h = hash;
int len = count;
if (h == 0 && len > 0) {
int off = offset;
char val[] = value;
for (int i = 0; i < len; i++) {
h = 31*h + val[off++];
}
hash = h;
}
return h;
}
test:
String s = "abc";
System.out.println(s.hashCode());//96354
String s2 = "abc";
System.out.println(s2.hashCode());//96354
Object obj = 1;
System.out.println(obj.hashCode());//1
Object obj2 = new Long(1);
System.out.println(obj.hashCode());//1
Object obj3 = new Long(1);
System.out.println(obj.equals(obj2));//false
System.out.println(obj==obj3);//false
System.out.println(obj2==obj3);//false
Object obj4 = 1;
System.out.println(obj==obj4);//true
Long obj5 = 1L; System.out.println(obj == obj5);//false System.out.println(obj4.equals(obj5));//false