Java基础:Obeject类的方法

Java基础

JDK 源码阅读顺序 https://blog.csdn.net/qq_21033663/article/details/79571506

Object 的方法:

参考:

  1. https://blog.csdn.net/weixin_37627441/article/details/79282056
  2. https://blog.csdn.net/qq_30264689/article/details/81903031
  3. https://www.cnblogs.com/yuxiaole/p/9570850.html
  4. https://www.oschina.net/question/82993_75533
  5. https://blog.csdn.net/xiangyuenacha/article/details/84255547
  6. https://www.cnblogs.com/shakinghead/p/7651502.html
  7. https://blog.csdn.net/hemeinvyiqiluoben/article/details/82990817
package java.lang;  
/** 
 * **Class {@code Object} is the root of the class hierarchy. 
 * Every class has {@code Object} as a superclass. All objects, 
 * including arrays, implement the methods of this class** 
 * 文档大概意思该类是所有类的基类,包括数组也实现了该类的方法 
 */  
public class Object {  
   //一个本地方法,具体是用C(C++)在DLL中实现的,然后通过JNI调用  
    private static native void registerNatives();  
    static {  
    //对象初始化时自动调用此方法  
        registerNatives();  
    }  
  
  
    /** 
     * Returns the runtime class of this {@code Object}.  
     //返回运行时类 
     */  
    public final native Class<?> getClass();  
  
  
   /** 
     * Returns a hash code value for the object.  
     //大概的意思就是返回int hashCode值 
     */  
    public native int hashCode();  
  
  
    /** 
     * Indicates whether some other object is "equal to" this one. 
     //比较两个对象内容是否相等 
     */  
    public boolean equals(Object obj) {  
        return (this == obj);  
    }  
  
  
    /** 
     * Creates and returns a copy of this object.  The precise meaning 
     * of "copy" may depend on the class of the object.  
     * 返回一个复制的对象 
     */  
    protected native Object clone() throws CloneNotSupportedException;  
  
  
    /** 
     * Returns a string representation of the object.  
     * 返回该对象的字符串表示 
     */  
    public String toString() {  
  
      return getClass().getName() + "@" +   Integer.toHexString(hashCode());  
    }  
  
  
    /** 
     * Wakes up a single thread that is waiting on this object's 
     *唤醒单个线程 
     */  
    public final native void notify();  
  
  
    /** 
     * Wakes up all threads that are waiting on this object's monitor.  
     * 唤醒所有线程 
     */  
    public final native void notifyAll();  
  
    /** 
     * Causes the current thread to wait until either another thread invokes the 
     * {@link java.lang.Object#notify()} method or the 
     * {@link java.lang.Object#notifyAll()} method for this object, or a 
     * specified amount of time has elapsed 
     */  
    public final native void wait(long timeout) throws InterruptedException;  
  
  
    /** 
     * Causes the current thread to wait until another thread invokes the 
     * {@link java.lang.Object#notify()} method or the 
     * {@link java.lang.Object#notifyAll()} method for this object, or 
     * some other thread interrupts the current thread, or a certain 
     * amount of real time has elapsed. 
     * 在其他线程调用此对象的 notify() 方法或 notifyAll() 方法, 
     * 或者其他某个线程中断当前线程,或者已超过某个实际时间量前,导致当前线程等待 
     */  
    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 >= 500000 || (nanos != 0 && timeout == 0)) {  
            timeout++;  
        }  
  
        wait(timeout);  
    }  
  
  
    /** 
     * Causes the current thread to wait until another thread invokes the 
     * {@link java.lang.Object#notify()} method or the 
     * {@link java.lang.Object#notifyAll()} method for this object. 
     * In other words, this method behaves exactly as if it simply 
     * performs the call {@code wait(0)} 
     */  
    public final void wait() throws InterruptedException {  
        wait(0);  
    }  
  
    /** 
     * Called by the garbage collector on an object when garbage collection 
     * determines that there are no more references to the object. 
     * A subclass overrides the {@code finalize} method to dispose of 
     * system resources or to perform other cleanup. 
     * 当垃圾回收器确定不存在对该对象的更多引用时,由对象的垃圾回收器调用此方法 
     */  
    protected void finalize() throws Throwable { }  
}  
  • hashCode方法:hashCode()方法被用来获取给定对象的唯一整数。这个整数被用来确定对象被存储在HashTable类似的结构中的位置。默认的,Object类的hashCode()方法返回这个对象存储的内存地址的编号。
  • equals方法:不重写 equals 方法,执行 obeject1.equals(object2) 比较的就是两个对象的地址(即 obeject1 == obeject2),肯定是不相等的(抛开基本数据类型)。其实当 equals 方法被重写时,通常有必要重写 hashCode 方法,以维护 hashCode 方法的常规协定,该协定声明相等对象必须具有相等的哈希码。
    参考文章:
    https://www.cnblogs.com/yuxiaole/p/9570850.html
    https://www.oschina.net/question/82993_75533
    https://blog.csdn.net/xiangyuenacha/article/details/84255547
  • clone方法:保护方法,实现对象的浅复制(浅拷贝),只有实现了Cloneable接口才可以调用该方法,否则抛出CloneNotSupportedException异常。
    浅拷贝深拷贝参考文章:
    https://www.cnblogs.com/shakinghead/p/7651502.html
  • toString方法:返回对象的字符串表达形式(对象名和hashcod码),这里不多说,是建议重写的。
  • notify、notifyAll、wait方法:不可被重写
    参考文章:
    https://blog.csdn.net/hemeinvyiqiluoben/article/details/82990817
  • finalize方法:当垃圾回收器确定不存在对该对象的更多引用时,由对象的垃圾回收器调用此方法。子类重写 finalize 方法,以配置系统资源或执行其他清除。 一旦垃圾回收器准备好释放对象占用的存储空间,将首先调用其finalize()方法,并且在下一次垃圾回收动作发生时,才会真正回收对象占用的内存。 对象可能不被垃圾回收。垃圾回收并不等于C++中的析构。垃圾回收只与内存有关。finalize可以用来 验证 对象终结条件。
    https://blog.csdn.net/Justin_zhao/article/details/74358828
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值