Home >Java >javaTutorial >In-depth analysis of the Object class (pictures and text)
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:
According to the comments we can summarize the following points:
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: If the hash values are not equal, then you can directly determine that the keys are not equal!
- To rewrite the
equals()
method, you must Override the method ofhashCode()
equals()
method defaults to the address of the comparison object and uses the==
equivalent operation CharacterhashCode()
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 objectequals()
The method also has 5 default principles:
- Reflexivity--->Calling
equals()
returns true, regardless of which of the two objects callsequals()
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)
andy.equals(z)
both return true, then we can conclude:x.equals(z)
Return true- Symmetry --->
x.equals(y)
andy.equals(x)
results should be equal.- The parameter passed in is null, and the returned value is false
toString
The toString method is mainly used to identify
- 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 and notify methods are actually Java’s APIs that provide us with communication between threads.
Why wait and notify are on the Object methodWhether 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()
wait()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- is awakened
- The thread was interrupted
wait()The thread calling
- wait()
- The time is up
notify()was awakened by
- notifyAll()
was
- Wake up
- will
release the lock
Thread.sleep() can be paused The current thread releases CPU control. ##But please note: the garbage collector was called before, but this method does not know when to call, and has uncertainty
and
Object.wait()
Object.wait()
releases the CPU while releasing the control of the object lock. Thread.sleep()
does not release the lock will not immediately obtain the lock object . Instead, wait for notify's synchronized code block to be executed before will obtain the lock object
finalizefinalize()` method will clear the object after 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.
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!