JDK源码解读之java.lang.Object

2020-03-28  本文已影响0人  方覆机
/**
 * Class Object is the root of the class hierarchy.
 * Every class has Object as a superclass. All objects,
 * including arrays, implement the methods of this class.
 *
 *
 * @author  unascribed
 * @see     java.lang.Class
 * @since   JDK1.0
 */

Object类的类层级的根部.Object是所有类的超类,所有对象包括数组,都实现了这个类的方法。

/**
 *一个本地方法,具体是用C(C++)在DLL中实现的,然后通过JNI调用
 */
private static native void registerNatives();
//对象初始化时自动调用此方法
static {
    registerNatives();
}
/*

Returns the runtime class of this Object. The returned Class object is the object that is 
locked by static synchronized methods of the represented class.

The actual result type is Class<? extends |X|> where |X| is the erasure of the static 
type of the expression on which getClass() is called.

*/
/**
 ** @return The  Class object that represents the runtime 
 *          class of this object.
 */
public final native Class<?> getClass();

返回Object运行类返回的对象被所代表类的静态同步方法锁住,getClass()调用之后的大体的结果类型为最初的类,附加的泛型发生了类型擦除。

/*
Returns a hash code value for the object.This method is supported for the benefit of hash tables 
such as those provided by java.util.HashMap.

The general contract of hashCode is: 
    Whenever it is invoked on the same object more than once during an execution of a 
Java application, the hashCode method must consistently return the same integer, 
provided no information used in equals 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.
    If two objects are equal according to the equals(Object) method, then calling the 
hashCode method on each of the two objects must produce the same integer result.
    It is not required that if two objects are unequal according to the 
java.lang.Object#equals(java.lang.Object) method, then calling the hashCode 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 hash tables.

As much as is reasonably practical, the hashCode method defined by class Object 
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&trade; programming language.)
*/
/**
 * @return  a hash code value for this object.
 * @see     java.lang.Object#equals(java.lang.Object)
 * @see     java.lang.System#identityHashCode
 */
 public native int hashCode();

返回这个对象的哈希值。这个方法支持散列表,比如HashMap。

hashCode的总体原则是:

​ 执行一个Java应用过程中,无论何时,相同对象无论调用多少次,hashCode()必须返回相同的整数,前提是这个对象在同级比较中的信息没有被修改。这个整数在相同应用的另一次执行时不再保留。

​ 如果两个对象根据equals()方法是相等的,这时分别调用hashCode()时,一定产生相同的整数结果。

​ 如果两个对象equals()的返回值不相等,这时调用hashCode()一定会产生两个不同的整数结果,是不必要的。但是编程者应该察觉到,不相等的对象产生的不同整数结果可能会提升散列表的性能。

按理来说,实际中多见的是,不同对象的定义的hashCode()的确会返回不同的整数。(典型的用例就是可以把对象内部的地址转换成整数,但是在Java编程语言是不必要的。)

/*
 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.HashMap
  */
    public boolean equals(Object obj) {
        return (this == obj);
    }

指出是否一些其他对象等于这一个。

equals()实现了一个等价的关系在非null对象引用上:

它是自反的:对于任意非null引用的x值,x.equals(x)应该返回true。

它是对称的:对于任意非null引用x值和y值,如果y.equals(x)返回true,那么x.equals(y)只能返回true。

它是可传导的:对于任意非null引用x、y和z,如果x.equals(y)返回true,y.equals(z)返回true,那么x.equals(z)返回true。

它是一致的:对于任意非null引用x和y,多次调用x.equals(y)连续返回true或连续返回false,前提是在该对象的同级比较中没有信息被修改。

对于任意非null引用x,x.equals(null)返回false。

类对象的equals()实现了最容易的区别类之间的相等关系的方法;

那就是对于任意非null引用x和y,如果x和y引用到相同的对象,难么这个方法返回true(x==y的值为true)。

需要注意的是,总的来说,无论何时,如果equals()被重写了,那么重写hashCode()是有必要的,以便于维持hashCode()整体的协议,也表明了相同的对象必须拥有相同的哈希值。

上一篇 下一篇

猜你喜欢

热点阅读