概述
文章目录
- 一、getClass
- 二、hashCode
- 三、equals
- 四、clone
- 五、toString
- 六、notify
- 七、notifyAll
- 八、wait
- 九、wait(long timeout)
- 十、wait(long timeout, int nanos)
- 十一、finalize()
一、getClass
1. 概述
用于获取对象的运行时对象的类
2. 源码
public final native Class<?> getClass();
3. 使用
Object obj = 对象/值;
Class<?> resu = obj.getClass();
二、hashCode
1. 概述
用于获取对象的 hash 值
2. 源码
public native int hashCode();
3. 使用
Object obj = 对象/值;
int has = obj.hashCode();
三、equals
1. 概述
比较两个对象是否相等
2. 源码
public boolean equals(Object obj) {
return (this == obj);
}
// 创建两个对象
Object obj1 = new Object();
Object obj2 = new Object();
// 判断 obj1 与 obj2 是否相等
// 不同对象,内存地址不同,不相等,返回 false
System.out.println(obj1.equals(obj2)); // false
// obj1 赋值给 obj3
// String 重写了 equals() 方法
// 对象引用,内存地址相同,相等,返回 true
Object obj3 = obj1;
System.out.println(obj1.equals(obj3)); // true
4. equals和==的区别
==
- 判断两个对象的地址是不是相等,即判断两个对象是不是同一个对象
- 基本数据类型比较的是值
- 引用数据类型比较的是内存地址
equals
- 一般来说比较的是两个对象是否相等,等同于"=="
- 不能用于比较基本数据类型的变量
- 若类重写equals方法,可以用来比较两个对象的内容是不是相等
四、clone
1. 概述
用于创建并返回一个对象的拷贝
clone方法是浅拷贝,对象内属性引用的对象只会拷贝引用地址,而不会将引用的对象重新分配内存,相对应的深拷贝则会连引用的对象也重新创建
2. 源码
protected native Object clone() throws CloneNotSupportedException;
3. 使用
对象类实现Cloneable接口
public class objectName implements Cloneable{
//对象中重写clone()方法
public Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
创建对象调用clone方法
//创建对象
objectName obj = new objectName();
//克隆对象
objectName obj1 = (objectName)obj.clone();
五、toString
1. 概述
返回对象的字符串表示形式
2. 源码
public String toString() {
return getClass().getName() + "@" + Integer.toHexString(hashCode());
}
3. 使用
Object obj = new Object();
String str = obj.toString();
System.out.println("返回对象的字符串表示形式 = " + obj);
六、notify
1. 概述
唤醒在该对象上等待的某个线程
2. 源码
public final native void notify();
七、notifyAll
1. 概述
唤醒在该对象上等待的所有线程
2. 源码
public final native void notifyAll();
八、wait
1. 概述
让当前线程进入等待状态。直到其他线程调用此对象的 notify() 方法或 notifyAll() 方法。
2. 源码
public final void wait() throws InterruptedException {
wait(0);
}
九、wait(long timeout)
1. 概述
让当前线程处于等待(阻塞)状态,直到其他线程调用此对象的 notify() 方法或 notifyAll() 方法,或者超过参数设置的timeout超时时间。
2. 源码
public final native void wait(long timeout) throws InterruptedException;
十、wait(long timeout, int nanos)
1. 概述
与 wait(long timeout) 方法类似,多了一个 nanos 参数,这个参数表示额外时间(以纳秒为单位,范围是 0-999999)。 所以超时的时间还需要加上 nanos 纳秒。。
2. 源码
public final void wait(long timeout, int nanos) throws InterruptedException {
if (timeout < 0) {
throw new IllegalArgumentException("timeout value is negative");
}
if (nanos < 0 || nanos > 999999) {
throw new IllegalArgumentException("nanosecond timeout value out of range");
}
if (nanos > 0) {
timeout++;
}
wait(timeout);
}
十一、finalize()
1. 概述
当 GC (垃圾回收器)确定不存在对该对象的有更多引用时,由对象的垃圾回收器调用此方法。
2. 源码
protected void finalize() throws Throwable { }
3. 使用
对象.finalize();
最后
以上就是慈祥鞋子为你收集整理的java基础 - 顶级父类:Object一、getClass二、hashCode三、equals四、clone五、toString六、notify七、notifyAll八、wait九、wait(long timeout)十、wait(long timeout, int nanos)十一、finalize()的全部内容,希望文章能够帮你解决java基础 - 顶级父类:Object一、getClass二、hashCode三、equals四、clone五、toString六、notify七、notifyAll八、wait九、wait(long timeout)十、wait(long timeout, int nanos)十一、finalize()所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复