Java makes an example analysis of object from the perspective of JDK source code
Object is the parent class of all classes, that is, all classes in Java inherit directly or indirectly from the object class. For example, you can create a classA casually. Although it is not explicitly stated, it defaults to extendsobject.
The last three points "..." Indicates that an indefinite number of parameters can be accepted. The old way of writing is objectargs [], but it is recommended in the new version of Java To show. for example
publicvoidgetSomething(String...strings)(){}
Object is the parent class of all classes in Java, that is, all classes, whether self created or in the system, inherit from the object class, that is, all classes can replace the object class in any case. According to the Richter replacement principle, a subclass can replace its parent class in any case, but a parent class may not replace its child class, Java often says that everything is an object, which is actually this truth! Object class embodies the four characteristics of polymorphism, inheritance, encapsulation and abstraction in OOP thought!
The object class is the base class of all classes, not the data type. You can query the JDK document to see that all classes inherit from object.
Object... The parameter definition of objects is a polymorphic expression in the case of uncertain method parameters. That is, this method can pass multiple parameters, and the number of parameters is uncertain. In this way, you need to do some processing in the method body. Because object is a base class, use object The parameter form of objects allows all objects inherited from object to be used as parameters. This method should be less used in practice.
The form of object [] obj is a parameter form composed of an object array. Note that the parameters of this method are fixed. It is an object array. The elements stored in this array can be objects of all classes inherited from object.
These basic things suggest you watch "thinkin Java" several times
Java's object is the parent class of all other classes. From the inheritance level, it is the top root, so it is also the only class without a parent class. It contains some methods commonly used by objects, such as getClass, hashcode, equals, clone, toString, notify, wait and so on. Therefore, after other classes inherit object, they do not need to implement these methods repeatedly. Most of these methods are native methods, which are analyzed in detail below.
The main codes are as follows:
Registernatives method
Because the registernatives method is decorated with a static block, it will be executed when the object class is loaded, and the corresponding local method is Java_ java_ lang_ Object_ Registernatives, as follows:,
You can see that it indirectly calls JNI nativeinterface_ The method of the structure can be seen simply as follows: what it does is roughly to correspond the method name of the Java layer to the local function to facilitate the execution engine to call C / C + + functions according to these corresponding tables when executing bytecode. As shown below, register these methods. When the execution engine executes the hashcode method, it can find the JVM of the JVM through the relational table_ Ihashcode function, where () I can also know that the type on the Java layer should be converted to int type. This mapping can actually be seen as mapping a string to a function pointer.
GetClass method
The getClass method is also a local method, and the corresponding local method is Java_ java_ lang_ Object_ GetClass, as follows:
So here we mainly look at the getobjectclass function. The class of Java layer corresponds to klassoop in C + + layer, so the metadata and method information about the class can be obtained through it.
Hashcode method
It can be seen from the registration of several local methods by the registernatives method above that the function corresponding to the hashcode method is the JVM_ Ihashcode, i.e
For hashcode, the generated logic is generated by synchronizer CPP get_ next_ The hash function determines that the implementation is complex. There are different generation strategies according to different values of hashcode. Finally, a hash mask is used for processing.
Equals method
This is a non local method, and the judgment logic is also very simple, direct = = comparison.
Clone method
It is known from the local method table that the local function corresponding to the clone method is the JVM_ Clone, the clone method is mainly used to clone objects, Generate the same new object based on the object (if the property of the object of our common class is of the original type, the value will be cloned, but if it is an object, the address of the object will be cloned). To clone a Java class, you need to implement the clonable interface. If (! Klass - > is_clonable()) here, you will check whether the interface is implemented. Then judge whether the array allocates memory space in two cases, and the new object is new_ Obj, then new_ Obj to set copy and C + + layer data structure. Finally, it is converted to jobobject type to facilitate conversion to object type of Java layer.
ToString method
The logic is to get the class name plus @ plus the hexadecimal hashcode.
Notify method
This method is used to wake up the thread. The final modifier cannot be rewritten. The corresponding local method is the JVM_ Monitornotify, objectsynchronizer:: notify will eventually call objectmonitor:: notify (traps). This process is that objectsynchronizer will try to get the freeobjectmonitor object from the current thread. If it is unsuccessful, it will try to get it from the global.
The objectmonitor object contains a_ Waitset queue object, which holds all threads in wait state, represented by objectwaiter object. What notify needs to do is to get it first_ Waitset queue lock, and then remove it_ The first objectwaiter object in the waitset queue, and then process the object according to different policies, such as adding it to_ Entrylist queue. Then release_ Waitset queue lock. It does not release the lock corresponding to synchronized, so the lock can only be released when the synchronized synchronization block ends.
NotifyAll method
Similar to the notify method, it is only fetching_ The waitset queue does not take the first but all.
Wait method
The wait method is to make the thread wait, and its corresponding local method is the JVM_ Monitorwait indirectly calls objectsynchronizer:: wait, which corresponds to notify. It is also the wait method of calling objectmonitor object. This method is long and will not be posted here. It is probably to create an objectwaiter object and then obtain_ Waitset queue lock adds the objectwaiter object to the queue, and then releases the queue lock. In addition, it also releases the lock corresponding to synchronized, so the lock is not released until the end of the synchronized synchronization block.
Finalize method
This method is used to call when the object is recycled. This method is supported by the JVM. The finalize method of the object does nothing by default. If the subclass needs to perform some logical processing when the object is recycled, the finalize method can be overridden
summary
The above is all about the instance analysis of object in Java from the perspective of JDK source code. I hope it will be helpful to you. Interested friends can continue to refer to other related topics on this site. If there are deficiencies, please leave a message to point out. Thank you for your support!