网站建设发展史/企业推广视频
看源码也不太懂怎么看,Object类的 java 代码很少,方法实现都靠 C/C++ ,主要看注释,然后自己理解。有不对的地方请指正
1. 概览
importjdk.internal.HotSpotIntrinsicCandidate;/*** native 关键字修饰的方法:
* 1. 没有方法体
* 2. 将调用C/C++实现的方法(可以搜索一下JNI)*/
public classObject {/*** 这个方法使JVM能找到本地的方法(C/C++实现的方法)*/
private static native voidregisterNatives();static{
registerNatives();
}/*** 空构*/@HotSpotIntrinsicCandidatepublicObject() {}/*** 关键字 native, 调用的是 C/C++ 实现的方法
* 关键字 final,不允许被子类重写
* 获得该对象运行时的Class类的实例
**/@HotSpotIntrinsicCandidatepublic final native Class>getClass();/*** 关键字 native
* 返回 对象的散列值
* 允许重写(常被重写)*/@HotSpotIntrinsicCandidatepublic native inthashCode();/***判断两个对象是否相等
*常被重写*/
public booleanequals(java.lang.Object obj) {return (this ==obj);
}/*** 创建并返回对象的一个副本(拷贝)
* 关键字 native
* 关键字 protected*/@HotSpotIntrinsicCandidateprotected native java.lang.Object clone() throwsCloneNotSupportedException;/****/
publicString toString() {return getClass().getName() + "@" +Integer.toHexString(hashCode());
}/*** 唤醒一个正在等待该资源的线程
* 关键字 final
* 关键字 native*/@HotSpotIntrinsicCandidatepublic final native voidnotify();/*** 唤醒所有正在等待该资源的线程
* 关键字 native
* 关键字 final*/@HotSpotIntrinsicCandidatepublic final native voidnotifyAll();/*** 使当前线程一直等待,直到它被唤醒. 通常 被 notify 或者 被 interrupt
* 关键字final
* 调用 wait(0L)*/
public final void wait() throwsInterruptedException {
wait(0L);
}/*** 使当前线程一直等待,直到它被唤醒. 通常 被 notify 或者 被 interrupt,或者超时
* 关键字 final
* 关键字 native*/
public final native void wait(long timeout) throwsInterruptedException;/*** 使当前线程一直等待,直到它被唤醒. 通常 被 notify 或者 被 interrupt,或者超时
* 关键字 final
* 参数 nanos 是只纳秒, 很蛋疼的是只要 0 < nanos <= 999999, timeout ++ , 也就是说根本没法精确到纳秒的级别*/
public final void wait(long timeout, int nanos) throwsInterruptedException {if (timeout < 0) {throw new IllegalArgumentException("timeout value is negative");
}if (nanos < 0 || nanos > 999999) {throw newIllegalArgumentException("nanosecond timeout value out of range");
}if (nanos > 0) {
timeout++;
}
wait(timeout);
}/*** 垃圾回收时调用*/@Deprecated(since="9")protected void finalize() throwsThrowable { }
}
2. 关于Object类: Object 类是所有类的父类, 所有类,包括数组,都实现了该类的方法。
3. getClass() 方法
public final native Class> getClass();
a. 该方法通过 final 关键字修饰,不允许被重写
b. 该方法通过 native 关键字修饰, 没有方法体,具体功能由 C/C++ 实现
c. 该方法返回一个 Class 类的实例。
The returned {@code Class} object is the object that is locked by {@codestatic synchronized} methods of the represented class. 这句话不太理解,好像是说 返回的 Class类的实例 是该对象所代表的类的静态同步方法(static synchronized methods)锁定的对象
d.实际返回的类型是 Class extends |X|> ,|X| 是指描述该对象的类的擦除类型,英文水平有限。。直接看例子
Number n = 0;
Class extends Number> nClass =n.getClass();
System.out.println(nClass);//class java.lang.Integer
|X| 是指 Number 类
4. hashcode() 方法
public native int hashCode();
a. 该方法返回对象的哈希值
b. 关键字 native
c. hashcode 的通俗约定:
1)在同一个 java 程序实现中,对于同一个对象,只要对象 用于 equals 方法比较的信息没被修改, 不管何时调用 hashcode 方法都应该返回相同的值。
2)对于不同的实现,对同一个对象,hashcode 不必返回相同的值。
3)如果两个对象在使用 equals 方法比较时返回 true,则它们调用 hashcode 方法必须返回相同的值。
4)如果两个对象在使用 equals 方法时返回 false, 则它们调用 hashcode 方法没有要求必须返回不同的整数值。但是在设计 hashcode 算法应该尽量返回不同的值(降低 hash 碰撞的概率);
5) 事实上, Object 实现的 hashcode 方法也没有实现 4)情况的预期。
5. equals(Object obj) 方法
public booleanequals(Object obj) {return (this ==obj);
}
a. 该方法用于判断两个对象是否相等
b. 默认的实现等同于 == 操作符
c. equals 方法约定:
1)自反性:对任何一个非空对象 x, x.equals(x) 返回 true
2) 对称性:对于任意两个非空对象 x 和 y。 如果 x.equals(y) 返回 true,那么 y.equals(x) 必须返回 true。
3)传递性,对于任何非空对象 x、y、z, 如果 x.equals(y), y.equals(z) 都返回 true, 那么 x.equals(z) 必须返回 true。
4)一致性:对于任何非空对象x, y,只要在这个过程中 x,y没有做任何修改,x.equals(y) 返回的值必须相同。
5)对于任何非空对象 x, x.equals(null) 必须返回 false。
d. 重写 equals 方法的同时,必须重写 hashcode 方法以保证 equals 为 true 的两个对象调用 hashcode 方法必须相等。
6. clone() 方法
protected native Object clone() throws CloneNotSupportedException;
b. 返回该对象的一个副本(拷贝)
a. native 关键字
c. 对任意非空对象 x:
1) x.clone() != x 的结果是 true
2)x.clone().getClass() == x.getClass()的结果不一定为 true。 很明显,因为这个方法定义返回的是 Object, 理论上你可以自己在重写的时候返回任何类的实例,但是一般都会返回同一个类的实例。
3)x.clone().equals(x) 也没有要求必须返回 true
4) 按照约定,返回的对象应该是调用 super.clone(), 如果一个类和它的所有父类都遵循这个约定(即都没有重写过 clone() 方法),x.clone().getClass() == x.getClass() 会返回 true.
5) 按照约定,返回的对象必须是独立于被 clone 对象的(不要返回它本身,不然就没有意义)。 注意对 clone 对象里的可变引用字段的处理。
6)如果自定义类没有实现Cloneable 接口, 调用 clone 方法将抛出CloneNotSupportedException
7)所有数组都实现了 Cloneable 接口并且可以调用 clone 方法。需要注意的是 数组的 clone 方法的实现是一个 “浅拷贝”,不是”深拷贝“。其实本质上还是 值传递和地址传递的区别。看以下测试代码
importjava.util.Arrays;public classCloneTest {public static void main(String[] args) throwsCloneNotSupportedException {//测试数组 clone
int[] nums = {0, 1, 2, 3, 4, 5};int[] cnums =nums.clone();
System.out.println(Arrays.toString(nums));//[0, 1, 2, 3, 4, 5]
System.out.println(Arrays.toString(cnums)); //[0, 1, 2, 3, 4, 5]
nums[4] = 8;
System.out.println(Arrays.toString(nums));//[0, 1, 2, 3, 8, 5]
System.out.println(Arrays.toString(cnums)); //[0, 1, 2, 3, 4, 5]
People p1= new People("a", 0);
People p2= new People("b", 1);
People p3= new People("c", 2);
People[] ps={p1, p2, p3};
People[] cps=ps.clone();
print(ps);//People{name='a', age=0} People{name='b', age=1} People{name='c', age=2}
print(cps); //People{name='a', age=0} People{name='b', age=1} People{name='c', age=2}
p1.setAge(100);
print(ps);//People{name='a', age=100} People{name='b', age=1} People{name='c', age=2}
print(cps); //People{name='a', age=100} People{name='b', age=1} People{name='c', age=2}
}static voidprint(Object[] objects) {for(Object obj : objects) {
System.out.print(obj+ " ");
}
System.out.println();
}
}classPeople{privateString name;private intage;public People(String name, intage) {this.name =name;this.age =age;
}publicString getName() {returnname;
}public voidsetName(String name) {this.name =name;
}public intgetAge() {returnage;
}public void setAge(intage) {this.age =age;
}
@OverridepublicString toString() {return "People{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
@Overrideprotected Object clone() throwsCloneNotSupportedException {return super.clone();
}protected voidsay(){
System.out.println("!!!");
}
}
7. toString() 方法不做什么介绍
8. notify() 方法
public final native void notify();
a. 关键字 final
b. 关键字 native
c. 唤醒一个正在等待该资源的线程:
1) 唤醒哪个线程是不确定的,任意的
2) 调用该方法的前提是当前线程必须持有该对象的 monitor, 否则抛出IllegalMonitorStateException
9. notifyAll() 方法
public final native void notifyAll();
a. 关键字 final
b. 关键字 native
c. 唤醒所有正在等待该资源的线程:,让他们去竞争这个资源,调用该方法的前提是当前线程必须持有该对象的 monitor, 否则抛出IllegalMonitorStateException
10. wait() 方法: wait() 方法是和 notify() 配合使用的