Java类源码解析

Java类源码解析

一、object类源码

1.类构造器

类构造器是创建Java对象的途径之一,通过new 关键字调用构造器完成对象的实例化,还能通过构造器对对象进行相应的初始化。一个类必须要有一个构造器的存在,如果没有显示声明,那么系统会默认创造一个无参构造器,在JDK的Object类源码中,是看不到构造器的,系统会自动添加一个无参构造器。可以通过:Object obj = new Object();构造一个Object类的对象。

/**
 * Constructs a new object.
 */
@IntrinsicCandidate
public Object() {}

2.equals()方法

/*
 * Indicates whether some other object is "equal to" this one.
 * 

* The {@code equals} method implements an equivalence relation * on non-null object references: *

    *
  • It is reflexive: for any non-null reference value * {@code x}, {@code x.equals(x)} should return * {@code true}. *
  • It is symmetric: 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}. *
  • It is transitive: 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}. *
  • It is consistent: 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. *
  • For any non-null reference value {@code x}, * {@code x.equals(null)} should return {@code false}. *
*

* 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}). *

* 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); }

equals()方法指示其他对象是否“等于”这个对象。a.equals(b)指的就是a是否等于b。
equals 方法在非空对象引用上实现等价关系:

  1. equals()是自反的:对于任何非空引用值 x,x.equals(x) 应该返回 true。
  2. equals()是对称的:对于任何非空引用值 x 和 y,当且仅当 y.equals(x) 返回 true 时,x.equals(y) 才应返回 true。
  3. equals()是可传递的:对于任何非空引用值 x、y 和 z,如果 x.equals(y) 返回 true 并且 y.equals(z) 返回 true,那么 x.equals(z) 应该返回 true。
  4. equals()是一致的:对于任何非空引用值 x 和 y,x.equals(y) 的多次调用始终返回 true 或始终返回 false,前提是对象的 equals 比较中使用的信息没有被修改。
  5. 对于任何非空引用值 x,x.equals(null) 应返回 false。

Object 类的 equals 方法实现了对象上最有区别的可能等价关系;也就是说,对于任何非空引用值 x 和 y,当且仅当 x 和 y 引用同一个对象(x == y 的值为 true)时,此方法才返回 true。

3.getClass()方法

/*
 * Returns the runtime class of this {@code Object}. The returned
 * {@code Class} object is the object that is locked by {@code
 * static synchronized} methods of the represented class.
 *
 * 

The actual result type is {@code Class} * where {@code |X|} is the erasure of the static type of the * expression on which {@code getClass} is called. For * example, no cast is required in this code fragment:

* *

* {@code Number n = 0; }
* {@code Class c = n.getClass(); } *

* * @return The {@code Class} object that represents the runtime * class of this object. * @jls 15.8.2 Class Literals */
@IntrinsicCandidate public final native Class<?> getClass();

getClass()作用是返回对象运行时的类。通过这个类对象我们可以获取该运行时类的相关属性和方法

4.hashCode()方法

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

* The general contract of {@code hashCode} is: *

    *
  • Whenever it is invoked on the same object more than once during * an execution of a Java application, the {@code hashCode} method * must consistently return the same integer, provided no information * used in {@code 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 {@code equals(Object)} * method, then calling the {@code 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 {@link java.lang.Object#equals(java.lang.Object)} * method, then calling the {@code 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. *
* * @implSpec * As far as is reasonably practical, the {@code hashCode} method defined * by class {@code Object} returns distinct integers for distinct objects. * * @return a hash code value for this object. * @see java.lang.Object#equals(java.lang.Object) * @see java.lang.System#identityHashCode */
@IntrinsicCandidate public native int hashCode();

返回某个对象的哈希值。

hashCode()要求
  1. 在 Java 应用程序执行期间,只要在同一个对象上多次调用它,hashCode 方法必须始终返回相同的整数,前提是在对象的 equals 比较中使用的信息没有被修改。该整数不需要从应用程序的一次执行到同一应用程序的另一次执行保持一致。
  2. 如果根据 equals(Object) 方法两个对象相等,则对两个对象中的每一个调用 hashCode 方法必须产生相同的整数结果。
  3. 如果根据 equals(Object) 方法两个对象不相等,则不需要对这两个对象中的每一个调用 hashCode 方法必须产生不同的整数结果。但是,为不相等的对象生成不同的整数结果可能会提高哈希表的性能。

两个对象相等,其 hashCode 一定相同。
hashCode 不相同的两个对象,一定不相等

5.toString()方法

/**
 * Returns a string representation of the object. In general, the
 * {@code toString} method returns a string that
 * "textually represents" this object. The result should
 * be a concise but informative representation that is easy for a
 * person to read.
 * It is recommended that all subclasses override this method.
 * 

* The {@code toString} method for class {@code Object} * returns a string consisting of the name of the class of which the * object is an instance, the at-sign character `{@code @}', and * the unsigned hexadecimal representation of the hash code of the * object. In other words, this method returns a string equal to the * value of: *

*
 * getClass().getName() + '@' + Integer.toHexString(hashCode())
 * 
* * @return a string representation of the object. */
public String toString() { return getClass().getName() + "@" + Integer.toHexString(hashCode()); }

toString()方法的作用是返回对象的字符串形式。通常, toString 方法返回一个“文本表示”此对象的字符串。 结果应该是一个简洁但信息丰富的表示,易于人们阅读。

getClass().getName()是返回对象的全类名(包含包名),Integer.toHexString(hashCode()) 是以16进制无符号整数形式返回此哈希码的字符串表示形式。

打印某个对象时,默认是调用 toString 方法,比如System.out.println(person),等价于 System.out.println(person.toString())

你可能感兴趣的:(java,object)