Object class of Java source code parsing
In the process of reading the source code, you can understand the ideas involved in others' realization of a function and see what they think and do. Next, let's take a look at the details of the object of Java source code parsing.
Java base class object
java. Lang. object, the parent class of all classes in Java. When you write a class, if there is no specified parent class (there is no explicit extends parent class), the compiler (usually the compiler completes this step) will add object as the parent class of the class by default (you can decompile the class to see its bytecode, but it seems that the decompiled javap built in Java 7 can't see it now).
More details: if class A does not explicitly inherit other classes, the compiler will add object as its parent class by default; If so, what about the explicit parent class? Either there is no explicit inheritance, then object is the parent of this parent class, which must also be the parent of class A. if so, and so on. Therefore, object is the ancestor class (parent class) of all classes in Java.
statement
1. This series is jdk1 7 (Oracle). If it is different from the source code you see, please compare the JDK version.
2. This series is my analysis of Java source code, but due to my limited level, I will not be able to fully interpret it. Even I can only say that I can do some superficial analysis through search, reading and learning. I hope I can give you advice and understanding.
Object source code
summary
Because object is the ancestor class of all classes in Java, all classes in java have methods in object. When looking at these methods, you should contact them. These methods are not for one class of objec, but all classes.
Since it is common to all classes, the commonness of all classes must be considered in the design. For example, the equals method is used to compare whether any two objects of the same type are equal, and toString is used to convert any object into a string for printing and viewing. Of course, the above methods are implemented by default. If you want to implement your own logic, you need to override it in your own class.
The above native methods cannot be seen in Oracle JDK, but the corresponding C / C + + code can be found in openjdk or other open source jdks. Detailed source code
1. Construction method
There is no object constructor in the source code, but similarly, the compiler will give object a default empty constructor during compilation (in fact, for all Java classes, as long as there is no constructor in the class, the compiler will give an empty constructor by default. If there is an existing constructor, it will not be added):
2.registerNatives
Native methods are all local methods. The so-called local methods are methods that are not implemented by the Java language, but these methods can be called through JNI like calling Java methods. You can search and view JNI in detail.
This method is used to register the following local methods of object (hashcode / clone / notify, etc.) (it can be understood that this method is to tell the JVM the implementation mapping of these local methods). Each local method will have this method, but its content is different (because the registered methods are different).
3.getClass
When each class is loaded, it will generate a class instance, and this method can obtain the class object of the object (the object here is the object in the heap, that is, the class of dynamic type) at runtime. The class object is mainly used for reflection.
4.hashCode
Get the hash value of the object. The Java virtual machine specification does not specify the specific implementation of this method, It only specifies that the int value returned by this method should be equal when the same object is called twice (under any condition) (but it does not specify that the hash values of two different objects must be different). The specific implementation is implemented by each JVM manufacturer, so the returned value does not have necessarily meaning (specifically referring to the hashcode method of object here), it may return the memory address of the object or the value calculated by a specific calculation formula.
5.equals
In principle or semantically, for the purpose of design, equals is used to compare whether two objects are equal. Here is what we usually understand: that is, whether the contents of two objects are equal, rather than whether the two objects are the same object in terms of program, that is, to compare their memory addresses; If you want to compare whether two objects are the same object (in this case, whether two references point to the same object), you can directly use = = to compare (= = compares the memory address of the object). However, the important thing here is that for object, it does not know how subclasses judge how their two instances are equal. Therefore, the default equals implementation compares the memory addresses of the two objects, that is, if subclasses do not override the equals method, its function is equivalent to = =.
It is said on the Internet that if you rewrite the equals method, you must rewrite the hashcode. In fact, if you are sure that there is no place similar to map, you do not have to rewrite the hashcode, because many methods of map are useful for the hashcode method to judge whether the two objects are equal. If you only use it to judge whether the two objects are equal, It is not necessary to rewrite hashcode (of course, it is also necessary to make sure that hashcode will not be used in other places, such as later use, others use, etc., but generally, it is recommended to rewrite hashcode method, so as to ensure that there will be no error anywhere).
If the hash values are not equal, the two objects must be unequal (not equal);
If the hash values are equal, the two objects are not necessarily equal (not necessarily equals).
If equals is equal, the hash value must be equal, that is, the necessary condition for equals to be equal when hash values are equal.
The hashcode method is generally used to judge the equals preconditions of two objects and to eliminate them. The reason for this is that the hashcode method is fast, and unequal ones can be rejected quickly. If the hashes are the same, the equals judgment will be called again.
6.clone
Clone an object. Clone an object with the same field values as the original object to obtain a new object. Note:
If you want to use this method, the object type must implement the clonable interface, otherwise an error will be reported. The reason is that the clone method of object verifies the object type. If it is not implemented, an error and throw exception will be reported;
The clone method returns a new object. This object is not created through the new instruction (unless you do not rewrite it through the clone method of object as described below), but through other instructions by the JVM;
Clones include deep clones and shallow clones, which are mainly divided for reference types in the middle of classes. For details, see Java clone deep parsing.
7.toString
ToString is a common method used by objects. Its meaning is to provide the function of formatting and outputting class fields in string form. Of course, similarly, objects cannot know the field information of subclasses. Therefore, the default toString output is: full path class name + @ + hash value.
If you want to output the field information of the class, you need to override the toString method to output the field information of the class in your own format.
8.notify/notifyAll/wait
These three methods are applicable to thread synchronization. Here we only briefly introduce their functions. For details, please refer to notify / notifyAll / wait.
Notify: randomly wakes up an object in the wait queue so that the thread that needs the object continues to execute;
NotifyAll: wakes up all objects in the queue
Wait: the object is in a waiting state. The thread that needs the object cannot continue to execute until the object is awakened by other threads calling the notify / notifyAll method.
9.finalize
The object is called before it is GC (garbage collection, see Java GC overview for details) (JVM calls actively). You can rewrite this method and take some actions before recycling this object. This method can only be called once for this object. Why do you say so? Objects are recycled and gone. Of course, it can only be called once? No, if you understand the principle of Java GC, you can know that if you are in the finalize method, If the object is given a strong reference again, the GC object will fail and the object will continue to survive. The next time the object becomes a recyclable object, the finalize method of the object will not be executed when the GC recycles the object.
The above is all about the object class of Java source code parsing in this paper. I hope it will be helpful to you. Interested friends can continue to refer to this site: Java Lang. void class source code analysis, talking about the wonderful use of future in Java multithreading (with source code), etc. if you have any questions, you can leave a message at any time. Xiaobian will reply to you in time. Thank you for your support for the programming tips website!