Home  >  Article  >  Java  >  In-depth analysis of the Object class (pictures and text)

In-depth analysis of the Object class (pictures and text)

不言
不言forward
2019-03-02 14:37:212730browse

This article brings you an in-depth analysis (pictures and texts) of the Object class. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

What are the methods of the Object class?

registerNatives() [Underlying implementation, not studied] hashCode()equals(Object obj)clone()toString() notify()notifyAll()wait(long timeout) [there are two overloads] finalize()Object has a total of 11 methods, one of which is the underlying implementation registerNatives(), two of which are wait() and wait(long timeout, int nanos) overloaded method. There is another attribute: return bytecode file object

hashCode

public native int hashCode();

Is implemented by the underlying method of native

equals

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

Determine the memory address directly

To make it clearer what they do, let’s read its comments:

In-depth analysis of the Object class (pictures and text)

In-depth analysis of the Object class (pictures and text)

According to the comments we can summarize the following points:

  • To rewrite the equals() method, you must Override the method of hashCode()
  • equals() method defaults to the address of the comparison object and uses the == equivalent operation Character
  • hashCode()The method has the function of improving performance for objects whose underlying layer is a hash table
  • The same object (if the object has not been modified): then call it repeatedly hashCode()Then the returned int is the same!
  • hashCode()The default method is converted from the address of the object
  • equals()The method also has 5 default principles:
    • Reflexivity--->Calling equals() returns true, regardless of which of the two objects calls equals()Everything is fine, all returned are true
    • Consistency--->As long as the object has not been modified, multiple calls will still return the corresponding results!
    • Transitiveness--->x.equals(y) and y.equals(z) both return true, then we can conclude: x.equals(z)Return true
    • Symmetry --->x.equals(y) and y.equals(x) results should be equal.
    • The parameter passed in is null, and the returned value is false
Why is it said that hashCode() uses a hash table as the bottom layer to improve performance? Easy to understand. Let's review the insertion of HashMap:

In-depth analysis of the Object class (pictures and text)

If the hash values ​​are not equal, then you can directly determine that the keys are not equal!

toString

The toString method is mainly used to identify

In-depth analysis of the Object class (pictures and text)

clone

  • The clone method is used to clone objects. Generally, the object you want to clone is independent (separate from the original object)
  • Deep copy refers to the All member variables of the object (if they are variable references) should be cloned. Shallow copy means that the member variables have not been cloned.
  • How to clone an object?

    • The cloned object must implement the Cloneable interface
    • override the clone method, preferably modified to public

wait & notify & notifyAll

wait and notify methods are actually Java’s APIs that provide us with communication between threads.

    Whether it is wait, notify or notifyAll(), it needs to be called
  • by the listener object (lock object)
    • Simply put:
    • They are all called in synchronized code blocks, otherwise an exception will be thrown!
  • notify() wakes up a thread waiting in the queue (not sure which one will be woken up), notifyAll( )What is awakened is the waiting queueAll threadsThere can be 4 situations in which the thread causing
  • wait()
  • is awakened
  • The thread was interrupted
    • wait()
    • The time is upwas awakened by
    • notify()
    • was
    • notifyAll()
    • Wake up
    The thread calling
  • wait()
  • will release the lock
Why wait and notify are on the Object method

  • Because our lock is an object lock [If you forget, you can review: Java lock mechanism to learn more], every object can become a lock. Let the current thread wait for the lock of an object. Of course, it should be operated through this object.
  • The lock object is arbitrary, so these methods must be defined in the Object class Both

Thread.sleep() and Object.wait()

can be paused The current thread releases CPU control.

  • The main difference is that Object.wait() releases the CPU while releasing the control of the object lock.
  • AndThread.sleep()does not release the lock

##But please note:

After the notify method is called, the awakened thread
will not immediately obtain the lock object . Instead, wait for notify's synchronized code block to be executed before will obtain the lock object
finalize

finalize()` method will clear the object after

the garbage collector was called before, but this method does not know when to call, and has uncertainty

Generally we will not rewrite it~
The finalize() method of an object
will only be called once , and being called finalize() does not mean that gc will immediately recycle the object, so it is possible that after finalize() is called, the object does not need to be recycled, and then when it is actually necessary to be recycled When recycling, because it has been called once before, finalize() will not be called, causing problems.

In-depth analysis of the Object class (pictures and text)

The above is the detailed content of In-depth analysis of the Object class (pictures and text). For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete