preface

The Object method in Java is a very high point in an interview, because Object is the “ancestor” of all classes. All classes in Java have a common ancestor, the Object class, and subclasses inherit public methods from all Object classes.

Let’s look at the class structure of Object (Alt +7) :

1. GetClass method

public final native Class<? > getClass();Copy the code
Final method, the runtime class object that gets the object. The class object is the object that describes the class to which the object belongs. This method is usually used in conjunction with the Java reflection mechanism.

2. The hashCode method

public native int hashCode();Copy the code
This method is mainly used to get the hash value of an object. This method returns the heap memory address of the Object by default.

3. The equals method

public boolean equals(Object obj) {        return(this == obj); }Copy the code
This method is used to compare two objects and returns true if both object references refer to the same object, false otherwise. Equals and == are different in general, but in Object they are the same. Subclasses typically override this method.

4. The clone method

protected native Object clone() throws CloneNotSupportedException;Copy the code
The method is to protect and realize the object of shallow copy, only implements the Cloneable interface to invoke the method, otherwise throw CloneNotSupportedException anomalies.

The default clone method is shallow copy. Shallow copy refers to the fact that the object referenced by an attribute in an object only copies the referenced address without reallocating the referenced object in memory. Deep copy recreates the referenced objects as well.

5. The toString method

public String toString() {
        return getClass().getName() + "@" + Integer.toHexString(hashCode());
}Copy the code
Returns a String object, usually overridden by subclasses. The default return format is: the class name of the object + @ + hashCode hexadecimal string.

6. Notify method

public final native void notify();Copy the code
The final method is used primarily to wake up a thread waiting on the object.

7. NotifyAll method

public final native void notifyAll();Copy the code
The final method is primarily used to wake up all threads waiting on the object.

8. Wait (long timeout

public final native void wait(long timeout) throws InterruptedException;Copy the code
The wait method causes the current thread to wait for the lock on the object. The current thread must be the owner of the object, that is, the lock on the object. The wait() method waits until the lock is acquired or interrupted. Wait (long timeout) Sets a timeout interval and returns if the lock has not been acquired within the specified time.

Wait (long timeout, int nanos

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);
}Copy the code
Parameters that

Timeout: Maximum wait time (milliseconds)

Nanos: Additional time in milliseconds range (0-999999)

This method causes the current thread to wait until another thread calls notify() or notifyAll() on this object, or at a specified time that has passed. This method is similar to an argument to the wait method, but it allows for better control over the amount of time spent waiting for a notification to abandon. Real-time quantity, calculated in nanoseconds, calculated by the following formula:

1000000 * timeout + nanosCopy the code
In all other respects, this method does the same thing as wait(long timeout). In particular, wait(0, 0) means the same as wait(0).

Methods 10. Wait

public final void wait() throws InterruptedException {  wait(0); }Copy the code
You can see that the wait() method actually calls the wait(long timeout) method, except that timeout is 0, i.e. no wait.

11. The finalize method

protected void finalize() throws Throwable { }Copy the code
This method is a protection method, mainly used to be called again during GC. If we implement this method, the object may be resurrected in this method to avoid GC collection.



The last

Welcome to pay attention to my public number [programmer chasing wind], the article will be updated in it, sorting out the data will be placed in it.