JDK源码之Object

mac2022-06-30  66

Object可以说是java中所有类的起源,每个类都会实现Object类的方法,以下内容为Object类源码的相关介绍.

一、类图结构

二、代码与注释

1 public class Object { 2 /** 3 * @Title: registerNatives 4 * @Description: 注册本地方法,关联类中的方法和本地方法 5 * 若未注册,则本地方法需要安装标准命名规则声明: 6 * registerNatives() ->Java_java_lang_Object_registerNatives() 7 * registerNatives本地方法中就是为了避免标准命名而注册这种联系 8 * @return void 返回类型 9 * @throws 10 */ 11 private static native void registerNatives(); 12 /** 13 * 静态代码块,类加载时会执行静态代码快 14 */ 15 static { 16 registerNatives(); 17 } 18 /** 19 * 获取一个对象的类对象,所谓类对象就是对类属性的抽象 相当于 Object.class 20 * 作用 可以通过获取类对象得到类的构造方法,字段属性,方法 以用于各种反射方法实现 21 * @Title: getClass 22 * @Description: <获取类对象> 23 * @return Class<?> 当前对象的类对象 24 * @throws 25 */ 26 public final native Class<?> getClass(); 27 28 /** 29 * 计算方法:根据对象的地址或者字符串或者数字算出来的整型数值 30 * 作用:增强hash表的性能 31 * hashCode方法与 equals方法联系: 32 * 两个对象相等 <->equals相同->hashCode相等 33 * @Title: hashCode 34 * @Description: <计算实例对象的hash值> 35 * @return int hashcode 36 * @throws 37 */ 38 public native int hashCode(); 39 40 /** 41 * 对于非空引用两个对象相等必须具有以下性质: 42 * 自反性:x.equals(x)==true 43 * 对称性:若 x.equals(y) 则 y.equals(x) 44 * 传递性:若 x.equals(y) && y.equals(z) 则 x.equals(z) 45 * 一致性:若equals方法未发生修改 则多次调用结果相同 46 * 对于任何非空引用值 x:x.equals(null) 都应返回 false 47 * @Title: equals 48 * @Description: <判断两个对象是否相等> 49 * @param obj 比较对象 50 * @return boolean 51 * @throws 52 */ 53 public boolean equals(Object obj) { 54 return (this == obj); 55 } 56 57 /** 58 * 这个是一个本地方法,创建并返回一个对象的副本(内存操作,效率高),通常这个对象的副本有以下特征: 59 * x.clone() != x 60 * x.clone().getClass() == x.getClass() 61 * 但 x.equals(x.clone()) 不一定成立: 62 * clone只会复制对象属性的值,不会复制属性中具有引用类型的对象,这属于一种浅复制 63 * 因此当类成员变量只包含基本类型或不可变引用时,上式成立, 64 * 当包含引用类型变量时,需要复制引用对象,即伸复制,才能保证 x.equals(x.clone()) == true 65 * @Title: clone 66 * @Description: <克隆一个对象的副本> 67 * @throws CloneNotSupportedException 参数 68 * @return Object 返回类型 69 * @throws 70 */ 71 protected native Object clone() throws CloneNotSupportedException; 72 73 /** 74 * getClass().getName() + '@' + Integer.toHexString(hashCode()) 75 * @Title: toString 76 * @Description: <返回对象的字符串表示> 77 * @return String 返回类型 78 * @throws 79 */ 80 public String toString() { 81 return getClass().getName() + "@" + Integer.toHexString(hashCode()); 82 } 83 /** 84 * 随机唤醒锁对象监视器上等待的某个线程 85 * 注意:执行此方法的线程必须具有该对象的监视器 86 * 对象监视器通过以下三种方法获得: 87 * 1、 88 * @Title: notify 89 * @Description: TODO(这里用一句话描述这个方法的作用) 参数 90 * @return void 返回类型 91 * @throws 92 */ 93 public final native void notify(); 94 95 /** 96 * 97 * @Title: notifyAll 98 * @Description: 唤醒在此对象监视器上等待的所有线程 99 * @return void 返回类型 100 * @throws 101 */ 102 public final native void notifyAll(); 103 104 /** 105 * 106 * @Title: wait 107 * @Description: 在其他线程调用此对象的 notify() 方法或 notifyAll() 方法, 108 * 或者超过指定的时间量前,导致当前线程等待。 109 * @param timeout 等待超时时间 110 * @throws InterruptedException 参数 111 * @return void 返回类型 112 * @throws 113 */ 114 public final native void wait(long timeout) throws InterruptedException; 115 116 /** 117 * 118 * @Title: wait 119 * @Description: timeout = 1000*1000*timeout+nanos 120 * @param timeout 超时时间 121 * @param nanos 额外时间 122 * @throws InterruptedException 参数 123 * @return void 返回类型 124 * @throws 125 */ 126 public final void wait(long timeout, int nanos) throws InterruptedException { 127 if (timeout < 0) { 128 throw new IllegalArgumentException("timeout value is negative"); 129 } 130 131 if (nanos < 0 || nanos > 999999) { 132 throw new IllegalArgumentException( 133 "nanosecond timeout value out of range"); 134 } 135 136 if (nanos > 0) { 137 timeout++; 138 } 139 140 wait(timeout); 141 } 142 143 /** 144 * 145 * @Title: wait 146 * @Description: 在其他线程调用此对象的 notify() 方法或 notifyAll() 方法前,导致当前线程等待 147 * @throws InterruptedException 参数 148 * @return void 返回类型 149 * @throws 150 */ 151 public final void wait() throws InterruptedException { 152 wait(0); 153 } 154 155 /** 156 * 157 * @Title: finalize 158 * @Description: 当垃圾回收器确定不存在对该对象的更多引用时,由对象的垃圾回收器调用此方法 159 * @throws Throwable 参数 160 * @return void 返回类型 161 * @throws 162 */ 163 protected void finalize() throws Throwable { } 164 } View Code

 

转载于:https://www.cnblogs.com/bookreader/p/10211636.html

最新回复(0)