当前位置:网站首页>What are the common methods of object

What are the common methods of object

2022-06-10 23:09:00 Li_ XiaoJin

Object Is the parent of all classes , Any class inherits by default Object.Object What methods do classes implement ?

(1)clone Method

Protection method , Implement shallow copy of objects , Only when Cloneable Interface to call this method , Otherwise throw CloneNotSupportedException abnormal .

(2)getClass Method

final Method , Get runtime type .

(3)toString Method

This method is used more , In general, subclasses have coverage .

(4)finalize Method

This method is used to release resources . Because it's not clear when the method will be called , Rarely used .

(5)equals Method

This method is a very important one . commonly equals and == It's different , But in Object The two are the same . Subclasses usually override this method .

(6)hashCode Method

This method is used for hash lookup , Rewrote equals Methods are usually rewritten hashCode Method . This method has hash function in some Collection It is used in .

In general, it must satisfy obj1.equals(obj2)==true. Can be launched obj1.hash- Code()==obj2.hashCode(), however hashCode Equality does not necessarily satisfy equals. But to improve efficiency , We should try to make the above two conditions close to equivalence .

(7)wait Method

wait The method is to make the current thread wait for the lock of the object , The current thread must be the owner of the object , That is, the lock with the object .wait() Method has been waiting for , Until the lock is obtained or interrupted .wait(long timeout) Set a timeout interval , If the lock is not obtained within the specified time, return .

After calling this method, the current thread goes to sleep , Until .

(1) Other threads called the object's notify Method .

(2) Other threads called the object's notifyAll Method .

(3) Other threads called interrupt Interrupt the thread .

(4) Time's up .

At this point, the thread can be scheduled , If it is interrupted, throw a InterruptedException abnormal .

(8)notify Method

This method wakes up a thread waiting on the object .

(9)notifyAll Method

This method wakes up all threads waiting on the object .


//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package java.lang;

public class ObjectTest {
    /**
     *  Construction method 
     */
    public Object() {
    }

    /**
     *   In order to make JVM Discover native features , They are named in a certain way .
     *   for example , about java.lang.Object.registerNatives,
     *   Corresponding C The function is named Java_java_lang_Object_registerNatives.
     *
     *   By using registerNatives( Or rather ,JNI function RegisterNatives), You can name anything you want C function .
     */
    private static native void registerNatives();

    /**
     * final Method , To get the type of runtime .
     *  This is what this method returns Object Class objects of objects / Runtime class object Class. Effect and Object.class identical .
     *
     *  Object obj = new Object();
     *  obj.getClass() == Object.class
     *
     */
    public final native Class<?> getClass();

    /**
     *  This method is used to return the physical address of the object where it is located ( Hash code value ),
     *  Often meet with equals Method to override at the same time , Make sure that two equal objects have equal hashCode.
     */
    public native int hashCode();

    /**
     * equals Used to compare whether the contents of two objects are equal .
     *  By default ( Inherited from Object class ),equals and == It's the same , Unless it's overwritten (override) 了 .
     */
    public boolean equals(Object var1) {
        return this == var1;
    }

    /**
     * clone() Function is used to save an existing object .
     *  Only when Cloneable Interface to call this method , Otherwise throw CloneNotSupportedException abnormal .
     */
    protected native Object clone() throws CloneNotSupportedException;

    /**
     * toString() Method returns the string representation of the object , There is nothing to say about this method .
     *  Usually overwritten 
     */
    public String toString() {
        return this.getClass().getName() + "@" + Integer.toHexString(this.hashCode());
    }

    /**
     *  Wake up a single thread waiting on this object monitor .(
     */
    public final native void notify();

    /**
     *  Wakes up all threads waiting on this object monitor .
     */
    public final native void notifyAll();

    /**
     *  Causes the current thread to wait , Until another thread calls the  notify()  Method or  notifyAll()  Method , Or more than the specified amount of time .
     */
    public final native void wait(long var1) throws InterruptedException;

    /**
     *  Causes the current thread to wait , Until another thread calls the  notify()  Method or  notifyAll()  Method ,
     *  Or some other thread breaks the current thread , Or has exceeded a certain amount of actual time .
     */
    public final void wait(long var1, int var3) throws InterruptedException {
        if (var1 < 0L) {
            throw new IllegalArgumentException("timeout value is negative");
        } else if (var3 >= 0 && var3 <= 999999) {
            if (var3 > 0) {
                ++var1;
            }

            this.wait(var1);
        } else {
            throw new IllegalArgumentException("nanosecond timeout value out of range");
        }
    }

    /**
     *   Causes the current thread to wait , Until another thread calls the  notify()  Method or  notifyAll()  Method .
     */
    public final void wait() throws InterruptedException {
        this.wait(0L);
    }

    /**
     *  When the garbage collector determines that there are no more references to the object , This method is called by the object's garbage collector .
     */
    protected void finalize() throws Throwable {
    }

    static {
        registerNatives();
    }
}

Copyright: use Creative Commons signature 4.0 International license agreement to license Links:https://lixj.fun/archives/about-object-method

原网站

版权声明
本文为[Li_ XiaoJin]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/161/202206102147101486.html