Java,求Object的equals源代码?

2025-05-05 20:10:08
推荐回答(5个)
回答1:

这个才是Object 的equals的源代码
/**
* Indicates whether some other object is "equal to" this one.
*


* The equals method implements an equivalence relation
* on non-null object references:
*


    *
  • It is reflexive: for any non-null reference value
    * x, x.equals(x) should return
    * true.
    *
  • It is symmetric: for any non-null reference values
    * x and y, x.equals(y)
    * should return true if and only if
    * y.equals(x) returns true.
    *
  • It is transitive: for any non-null reference values
    * x, y, and z, if
    * x.equals(y) returns true and
    * y.equals(z) returns true, then
    * x.equals(z) should return true.
    *
  • It is consistent: for any non-null reference values
    * x and y, multiple invocations of
    * x.equals(y) consistently return true
    * or consistently return false, provided no
    * information used in equals comparisons on the
    * objects is modified.
    *
  • For any non-null reference value x,
    * x.equals(null) should return false.
    *

*


* The equals method for class Object implements
* the most discriminating possible equivalence relation on objects;
* that is, for any non-null reference values x and
* y, this method returns true if and only
* if x and y refer to the same object
* (x == y has the value true).
*


* Note that it is generally necessary to override the hashCode
* method whenever this method is overridden, so as to maintain the
* general contract for the hashCode method, which states
* that equal objects must have equal hash codes.
*
* @param obj the reference object with which to compare.
* @return true if this object is the same as the obj
* argument; false otherwise.
* @see #hashCode()
* @see java.util.Hashtable
*/
public boolean equals(Object obj) {
return (this == obj);
}

回答2:

/**
* 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 = count;
if (n == anotherString.count) {
char v1[] = value;
char v2[] = anotherString.value;
int i = offset;
int j = anotherString.offset;
while (n-- != 0) {
if (v1[i++] != v2[j++])
return false;
}
return true;
}
}
return false;
}

//如有疑问加入JAVA/JS学习群50812460
找acc

回答3:

路径:...\jdk1.5.0_12\ 内的src.zip文件里面是java的源代码,你自己找着看吧,所有都在里面!

回答4:

public boolean equals(Object obj) {
return (this == obj);
}

回答5:

去中国代码库