Java-Object类详解

Object类是Java中的核心类,所有类默认继承自它。它包含如hashCode()、equals()、clone()、toString()、wait()、notify()和notifyAll()等基本方法。这些方法分别用于获取对象的哈希码、比较对象、复制对象、转换为字符串、线程同步及等待/唤醒操作。
摘要由CSDN通过智能技术生成

本文介绍Object类

前言

Object介绍

Object类是Javajava.lang包下的核心类,Java 允许把任何类型的对象赋给 Object 类型的变量。当一个类被定义后,如果没有指定继承的父类,那么默认父类就是 Object 类。

Code

package java.lang;

import jdk.internal.vm.annotation.IntrinsicCandidate;
public class Object {

    /**
     * Constructs a new object.
     */
    @IntrinsicCandidate
    public Object() {}
    
	@IntrinsicCandidate
    public final native Class<?> getClass();

	@IntrinsicCandidate
    public native int hashCode();

	public boolean equals(Object obj) {
        return (this == obj);
    }

	@IntrinsicCandidate
    protected native Object clone() throws CloneNotSupportedException;

	public String toString() {
        return getClass().getName() + "@" + Integer.toHexString(hashCode());
    }

	@IntrinsicCandidate
    public final native void notify();

	@IntrinsicCandidate
    public final native void notifyAll();

	public final void wait() throws InterruptedException {
        wait(0L);
    }

	public final native void wait(long timeoutMillis) throws InterruptedException;

	public final void wait(long timeoutMillis, int nanos) throws InterruptedException {
        if (timeoutMillis < 0) {
            throw new IllegalArgumentException("timeoutMillis value is negative");
        }

        if (nanos < 0 || nanos > 999999) {
            throw new IllegalArgumentException(
                                "nanosecond timeout value out of range");
        }

        if (nanos > 0 && timeoutMillis < Long.MAX_VALUE) {
            timeoutMillis++;
        }

        wait(timeoutMillis);
    }

	@Deprecated(since="9")
    protected void finalize() throws Throwable { }

Object类中的方法

  • getClass():native方法,用于返回当前运行时对象的Class对象,使用了final关键字修饰,故不允许子类重写。
  • hashCode() :native方法,用于返回对象的哈希码,主要使用在哈希表中,比如JDK中的HashMap。
  • equals(Object obj):用于比较2个对象的内存地址是否相等,String类对该方法进行了重写用户比较字符串的值是否相等。
  • clone() :naitive方法,用于创建并返回当前对象的一份拷贝。一般情况下,对于任何对象 x,表达式 x.clone() != x 为true,x.clone().getClass() == x.getClass() 为true。Object本身没有实现Cloneable接口,所以不重写clone方法并且进行调用的话会发生CloneNotSupportedException异常。
  • toString():返回类的名字@实例的哈希码的16进制的字符串。建议Object所有的子类都重写这个方法。
  • notify():native方法,并且不能重写。唤醒一个在此对象监视器上等待的线程(监视器相当于就是锁的概念)。如果有多个线程在等待只会任意唤醒一个。
  • notifyAll():native方法,并且不能重写。跟notify一样,唯一的区别就是会唤醒在此对象监视器上等待的所有线程,而不是一个线程。
  • wait(long timeout) :native方法,并且不能重写。暂停线程的执行。注意:sleep方法没有释放锁,而wait方法释放了锁 。timeout是等待时间。
  • wait(long timeout, int nanos) :多了nanos参数,这个参数表示额外时间(以毫微秒为单位,范围是 0-999999)。 所以超时的时间还需要加上nanos毫秒。
  • wait() :跟之前的2个wait方法一样,只不过该方法一直等待,没有超时时间这个概念
  • finalize() :实例被垃圾回收器回收的时候触发的操作
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

猫哥说

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值