Object is the parent class of all classes, and any class inherits Object by default. What methods does the Object class implement?
1. clone method
Protection method to implement shallow copying of objects. This method can only be called by implementing the Cloneable interface, otherwise a CloneNotSupportedException exception will be thrown.
2. getClass method
Final method, obtain the runtime type.
3. toString method
This method is used more frequently and generally has override of subclasses.
4. Finalize method
This method is used to free up resources. Because it is impossible to determine when the method is called, it is rarely used.
5. equals method
This method is a very important method. Generally equals and == are different, but in Object the two are the same. Subclasses generally need to rewrite this method.
6. hashCode method
This method is used for hash search. If the equals method is rewrite, the hashCode method is generally rewrite. This method is used in some collections with hashing functions.
Generally, obj1.equals(obj2)==true must be met. Obj1.hash-Code()==obj2.hashCode() can be introduced, but equal hashCode does not necessarily satisfy equals. However, in order to improve efficiency, the above two conditions should be kept close to equivalent.
7. Wait method
The wait 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 of the object. The wait() method waits until the lock is obtained or is interrupted. wait(longtimeout) sets a timeout interval and returns if no lock is obtained within the specified time.
After calling this method, the current thread goes to sleep until the following event occurs.
(1) Other threads call the object's notify method.
(2) Other threads call the object's notifyAll method.
(3) Other threads call interrupt to interrupt the thread.
(4) The time interval has arrived.
At this time, the thread can be scheduled. If it is interrupted, an InterruptedException will be thrown.
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.
―Object―
ClassObjective theroot for the classification archy.Every classshasObjectasa superclass.Allobjects, including arrays, implement themethod for this class.--FromOracle
―Explanation―
The Object class is the parent class inherited by all objects in Java, and even an array inherits the parent class (it can be understood as the original class, the ancestor of all classes. You may want to ask: Is the first class written by James an Object?).
All classes inherit the Object class implicitly, so they cannot be seen.
―Object―
Default constructor
―clone―
―equals―
Indicates whethersomeotherobjectis"equalto"thisone.
Theequalsmethodimmplions sanequivalence onnon-nullobjectreferences:—FromORacle—
The equals of the original class Object compares references to non-empty objects of two variables.
Source code:
public boolean equals(Object obj) { return (this == obj); }Through the source code, we can see that the original class equals is actually equivalent to "==".
―finalize―
―getClass―
―hashcode―
IntheJava programming language,everyclassimplicitlyorexplicitlyprovideshashCode() method, which digeststhedatastoredinan instance of theclassintoasinglehashvalue(a32-bitsignedinteger).Thishashisusedbyothercode whenstoringormanipulating theinstancethevaluesareintendedtobeevenlydistributedforvariedinputs foruseinclustering.Thispropertyisimportant to the performance of hashtablesandotherdatastructures thatstoreobjectsgroups("buckets")basedontheircomputehashvalues.Technically,inJava,hashCode() by defaultisanativemethod,meaning,ithasthemodifier'native',asitisimplementeddirectlyinthenativecodeintheJVM.
Source: Wikipedia
Each class in Java implements the hashcode method of Object implicitly or explicitly.
To summarize with Google and the official personal, why does the author have hashcode in the original class?
①. Storage optimization of class objects to facilitate searching of class objects.
②. Use with equals.
Note: Many blogs say that the hashcode method returns the physical storage address or logical storage address of the class. This statement is wrong. According to the official statement, the returned 32-bit value is only related to the storage location of the class object.
―notify―
―notifyall―
―toString―
The toString method for class object turn forwards consisting of the name of the class of which the object san instance, theat-signcharacter`@', and the unsigned hexa decimal representation of the hash code of the object.Inotherwords, this method return threshold equal to the value of:
getClass().getName()+'@'+Integer.toHexString(hashCode())
Source code:
public String toString() { return getClass().getName() + "@" + Integer.toHexString(hashCode()); }Returns a hash value in the format class name +@+ of the class.
―wait―
finalize()
Summarize
The above is the entire content of this article about java.lang.Object reading of java source code. I hope it will be helpful to everyone. Interested friends can continue to refer to other related topics on this site. If there are any shortcomings, please leave a message to point it out. Thank you friends for your support for this site!