[Source code analysis] The source of all phenomena in Java - Object

Mentioned Java, it would have to say Objectclass. In the interview, you may also often encounter Objectsimilar questions, which are summarized here for your reference only.

ObjectClass is Javathe root class of all classes in , that is, all classes inherit from Objectclass. It includes the following methods:

protected Object clone() throws CloneNotSupportedException

A copy class that creates and returns an object. In general for any object x,
-returned x.clone() != xas true
-returned x.clone().getClass() == x.getClass()as true
-returned x.clone().equals(x)astrue

If the object needs to be used clone, it needs to implement the Cloneableinterface. CloneableAn interface is just a marker, it doesn't have any methods.

For a more in-depth understanding, please refer to https://www.cnblogs.com/Qian123/p/5710533.html for object cloning and replication (including deep copy, shallow copy, etc.)

public boolean equals(Object obj)

Determines whether two objects are the same object. equalsMethods cannot act on nullobjects and have the following characteristics:
- Reflexive

For any non-null reference value x, always exists x.equals(x)as true
-symmetry

For any non-null reference value x, , must also return if it yexists - transitivityx.equals(y)truey.equals(x)true

For any non-null reference value x, y, z, exists x.equals(y)as true, then y.equals(z)also as true, x.equals(z)must also return true
- Consistency

For any non-null reference value x, always returns y, regardless of how many times it is called , or - For any non-null reference value , always exists asx.equals(y)truefalse
xx.equals(null)false

When overriding equals()a method, you should also override the hashCode()method

In the interview, I often ask "please talk about Objectthe difference between the method ==and equals()the method":
in fact, Objectthere is no difference between the two in the class, and both indicate whether the memory addresses pointed to by the two objects are the same. However, due Javato the polymorphism feature, subclasses can override equals()methods. For example, the Stringclass equals()represents whether the value of two strings is the same.

protected void finalize()

This method is called when the GC (garbage collector) determines that the object does not have any references. Subclasses can override this method to free system resources or perform other cleanup.

The JVM virtual machine cannot determine which thread to call the finalize()method of the object, but it can be sure that finalize()the thread calling the method will not block the user thread. If an exception is thrown while executing the method, the method of the corresponding object finalize()terminates execution. Also, each object will only execute the finalize()method once.

public final Class<?> getClass()

Returns the class of this object at runtime. The returned class object is staticthe object locked by the lock method.

int hashCode()

Return the hash value for the object. The conventions are as follows:
- If the same object is called multiple times during the same execution of the program, the hash value must return the same. But not the same calling process is not necessarily the same;
- If the two objects call equals()the same method, the hashCode()method return value must be the same;
- If the two objects call equals()the method is not the same, the hash value of the two objects does not need to be different. But programming must be aware that different objects produce different hash values ​​can improve HashTablethe performance of a class like this.

void notify()

Wake up a single thread waiting for an object lock. If there are multiple threads waiting, one of them will be woken up randomly, competing for locks with other threads that want to occupy the object lock.

void notifyAll()

Wake up all threads waiting for an object lock. The awakened thread competes for the lock with other threads that want to hold the object lock.

String toString()

Returns the Stringrepresentation of the object, default is getClass().getName() + '@' + Integer.toHexString(hashCode()). JDK specification requirements: Generally, in order to clearly print out object properties, it is recommended to override this method.

In the usual project, the author will write a Baseclass, override the toStringmethod, and return a JSONstring. Make all custom classes inherit from Basethis class .

void wait()

Called by the current thread that occupies the object lock, and releases the owned object lock until another thread calls notify()or the notifyAll()method wakes up.

When a thread calls a wait()method, there may be false wakeups, so this method must be used in a loop:

synchronized (obj) {
    while (<condition does not hold>)
        obj.wait();
    // Perform action appropriate to condition
}

void wait(long timeout)

It is called by the current thread that occupies the object lock, and the owned object lock is released until other threads call notify()or notifyAll()method wakes up or automatically wakes up after a specified time.

void wait(long timeout, int nanos)

It is called by the current thread that occupies the object lock, and the owned object lock is released until other threads call notify()or notifyAll()methods wake up or automatically wake up after the specified time (1000000*timeout + nanos).

Extended reading

  1. [Effective Java] Avoid using the finalize method
  2. [Effective Java] Please follow general conventions when overriding equals
  3. [Effective Java] The hashCode method must be rewritten when the equals method is rewritten
  4. [Effective Java] Carefully override the clone method

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326008149&siteId=291194637