一、getClass
- public final native Class<?> getClass()
- 意义:返回此Object运行时的类
- 作用:常用于反射和类型检查
- 使用场景:当需要知道一个对象的实际类型时,可以使用。
public class GetClassExample {
public static void main(String[] args) {
GetClassExample example = new GetClassExample(); // 创建对象
// 打印对象的运行时类
System.out.println("对象的类是: " + example.getClass().getName());
}
}
二、hashCode
- public native int hashCode()
- 意义:返回该对象的哈希码值
- 作用:在哈希表中确定对象的存储位置。
- 使用场景:需要自定义对象作为哈希表的键时,需要重写hashcode方法。
public class HashCodeExample {
private int id; // 对象的标识符
public HashCodeExample(int id) {
this.id = id; // 初始化标识符
}
@Override
public int hashCode() {
return id; // 返回对象的哈希码值
}
public static void main(String[] args) {
HashCodeExample example = new HashCodeExample(123); // 创建对象并设置ID
// 打印对象的哈希码值
System.out.println("对象的哈希码是: " + example.hashCode());
}
}
三、equals
- public boolean equals(Object obj)
- 意义:某个对象是否与此对象相等。
- 作用:比较两个对象是否相等。
- 使用场景:当需要自定义对象相等逻辑时,需要重写equals方法(重写equals方法,同时也要重写hashcode,要保证它们一致性)。
public class EqualsExample {
private int id; // 对象的标识符
public EqualsExample(int id) {
this.id = id; // 初始化标识符
}
@Override
public boolean equals(Object obj) {
if (this == obj) return true; // 如果两个对象引用相同,返回true
if (obj == null || getClass() != obj.getClass()) return false; // 如果对象为null或类不同,返回false
EqualsExample that = (EqualsExample) obj; // 类型转换
return id == that.id; // 比较标识符
}
public static void main(String[] args) {
EqualsExample example1 = new EqualsExample(123); // 创建第一个对象
EqualsExample example2 = new EqualsExample(123); // 创建第二个对象
// 打印两个对象是否相等
System.out.println("对象是否相等: " + example1.equals(example2));
}
}
四、clone
- public native Object clone()
- 意义:创建并返回此对象的一个副本
- 作用:实现对象的浅拷贝
- 使用场景:当需要复制一个对象,但是不希望两个对象引用同一个内存地址时去使用。并且 clone方法是受保护的, 需要先实现一个Cloneable接口,再去重写clone方法, 调用super.clone()。
public class CloneExample implements Cloneable {
private int id; // 对象的标识符
public CloneExample(int id) {
this.id = id; // 初始化标识符
}
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone(); // 调用父类的clone方法
}
public static void main(String[] args) {
try {
CloneExample example1 = new CloneExample(123); // 创建对象
CloneExample example2 = (CloneExample) example1.clone(); // 克隆对象
// 打印克隆对象的ID
System.out.println("克隆对象的ID是: " + example2.id);
} catch (CloneNotSupportedException e) {
e.printStackTrace(); // 捕获并打印异常
}
}
}
五、toString
- public String toString()
- 意义:返回该对象的字符串表示
- 作用:方便打印对象信息或转换为字符串。
- 使用场景:当需要打印对象信息或将对象转换为字符串时,通常需要重写toString方法。
public class ToStringExample {
private int id; // 对象的标识符
public ToStringExample(int id) {
this.id = id; // 初始化标识符
}
@Override
public String toString() {
return "ToStringExample{id=" + id + '}'; // 返回对象的字符串表示
}
public static void main(String[] args) {
ToStringExample example = new ToStringExample(123); // 创建对象
// 打印对象的字符串表示
System.out.println("对象的字符串表示: " + example.toString());
}
}
六、notify
- public final native void notify()
- 意义:唤醒在此对象监视器上等待的单个线程。
- 作用:用于多线程同步,实现线程间的通信。
- 使用场景:当线程调用一个对象的 wait方法,另一个线程可以通过使用notify()和notifyAll方法来唤醒等待的线程。
class NotifyExample {
private final Object lock = new Object(); // 锁对象
public void waitingThread() {
synchronized (lock) {
try {
System.out.println(Thread.currentThread().getName() + " 正在等待...");
lock.wait(); // 进入等待状态
System.out.println(Thread.currentThread().getName() + " 已经恢复!");
} catch (InterruptedException e) {
Thread.currentThread().interrupt(); // 处理中断异常
}
}
}
public void notifyingThread() {
synchronized (lock) {
lock.notify(); // 唤醒一个等待线程
System.out.println(Thread.currentThread().getName() + " 已通知一个线程.");
}
}
public static void main(String[] args) throws InterruptedException {
NotifyExample example = new NotifyExample(); // 创建示例对象
Thread t1 = new Thread(example::waitingThread, "线程-1");
Thread t2 = new Thread(example::waitingThread, "线程-2");
t1.start(); // 启动第一个线程
t2.start(); // 启动第二个线程
Thread.sleep(1000); // 确保两个线程进入等待状态
new Thread(example::notifyingThread, "通知线程").start(); // 启动通知线程
}
}
七、notifyAll
- public final native void notifyAll()
- 意义:唤醒在此对象监视器上等待的所有线程。
- 作用:用于多线程同步,实现线程间的通信。会唤醒所有等待的线程。
- 使用场景:唤醒所有线程。
class NotifyAllExample {
private final Object lock = new Object(); // 锁对象
public void waitingThread() {
synchronized (lock) {
try {
System.out.println(Thread.currentThread().getName() + " 正在等待...");
lock.wait(); // 进入等待状态
System.out.println(Thread.currentThread().getName() + " 已经恢复!");
} catch (InterruptedException e) {
Thread.currentThread().interrupt(); // 处理中断异常
}
}
}
public void notifyingThread() {
synchronized (lock) {
lock.notifyAll(); // 唤醒所有等待线程
System.out.println(Thread.currentThread().getName() + " 已通知所有线程.");
}
}
public static void main(String[] args) throws InterruptedException {
NotifyAllExample example = new NotifyAllExample(); // 创建示例对象
Thread t1 = new Thread(example::waitingThread, "线程-1");
Thread t2 = new Thread(example::waitingThread, "线程-2");
t1.start(); // 启动第一个线程
t2.start(); // 启动第二个线程
Thread.sleep(1000); // 确保两个线程进入等待状态
new Thread(example::notifyingThread, "通知线程").start(); // 启动通知线程
}
}
八、wait
- public final native void wait(long timeout);
- 意义:使当前线程等待直到其他线程调用此对象的notify或notifyAll方法。
- 作用:用于多线程同步,使线程进入等待状态。
- 使用场景:当需要再多线程环境下实现等待/通知机制使用wait 方法。
class WaitTimeoutExample {
private final Object lock = new Object(); // 锁对象
public void waitingThread() {
synchronized (lock) {
try {
System.out.println(Thread.currentThread().getName() + " 正在等待 2 秒...");
lock.wait(2000); // 等待2秒
System.out.println(Thread.currentThread().getName() + " 已经恢复!");
} catch (InterruptedException e) {
Thread.currentThread().interrupt(); // 处理中断异常
}
}
}
public static void main(String[] args) throws InterruptedException {
WaitTimeoutExample example = new WaitTimeoutExample(); // 创建示例对象
Thread t1 = new Thread(example::waitingThread, "线程-1");
t1.start(); // 启动线程
t1.join(); // 等待线程结束
}
}
九、wait
- public final native void wait(long timeout, int nanos)
- 意义:提供了更为精准的等待时间
- 作用:用于多线程同步,使线程进入等待状态。
- 使用场景:
- 高精度定时等待:在线程非常精确的时间后被唤醒。
- 等待特定事件发生:在特定事件中发生,并且等待时间也需要精确控制。
- 同步于协调:同步和协调多个线程的执行,使得线程能够在特定时间上得到控制。
class WaitNanosExample {
private final Object lock = new Object(); // 锁对象
public void waitingThread() {
synchronized (lock) {
try {
System.out.println(Thread.currentThread().getName() + " 正在等待 2 秒和 500 纳秒...");
lock.wait(2000, 500); // 等待2秒和500纳秒
System.out.println(Thread.currentThread().getName() + " 已经恢复!");
} catch (InterruptedException e) {
Thread.currentThread().interrupt(); // 处理中断异常
}
}
}
public static void main(String[] args) throws InterruptedException {
WaitNanosExample example = new WaitNanosExample(); // 创建示例对象
Thread t1 = new Thread(example::waitingThread, "线程-1");
t1.start(); // 启动线程
t1.join(); // 等待线程结束
}
}
十、finalize
-
- 意义:当垃圾回收器确定不存在对该对象的更多引用时,由对象的垃圾回收器调用此方法。
- 作用:允许对象在垃圾回收之前执行清理操作。
- 使用场景:
- 资源释放:在对象被销毁之前释放资源,比如关闭文件、数据库链接、网络连接。
- 临时文件删除:删除对象创建的临时文件,确保在对象销毁时不留下临时文件。
- 非内存资源清理:处理非内存资源的清理工作,释放一些操作系统资源。
public class FinalizeExample {
@Override
protected void finalize() throws Throwable {
// 在垃圾回收之前执行清理操作
System.out.println("对象正在被垃圾回收");
}
public static void main(String[] args) {
FinalizeExample example = new FinalizeExample(); // 创建对象
example = null; // 将对象置为null,使其可被垃圾回收
System.gc(); // 请求垃圾回收
// 给垃圾回收一些时间运行
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
Thread.currentThread().interrupt(); // 处理中断异常
}
}
}