Basic Description:
(1) The object class is located in the Java.lang package, and the Java.lang package contains Java's most basic and core classes, which are automatically imported at compile time;
(2) The object class is the ancestor of all Java classes. Each class uses Object as a superclass. All objects, including arrays, implement methods of this class. You can use a variable of type object to point to any type of object
The main methods of object are introduced:
Package Java.lang; public class Object { /* A local method, specifically implemented in a DLL with C (c + +), and then invoked through JNI. * /private static native void Registernatives (); /* This method is called automatically when the object is initialized * /static { registernatives (); } /* Returns the run-time class for this Object. * /public final native class<?> GetClass (); public native int hashcode ();
Hashcode () Method:
Hash value: The Hashcode method in Java is to map the object-related information (such as the object's storage address, the object's field, and so on) to a value according to certain rules, which is called a hash value.
Scenario: Consider a situation when inserting an object into a collection, how do you determine if the object already exists in the collection? (Note: Duplicate elements are not allowed in the collection.)
Most people would have thought of calling the Equals method to make comparisons on a case by case basis. However, if there are already 10,000 or more data in the collection, if the Equals method is used to compare each other, efficiency must be a problem. At this point the function of the Hashcode method is reflected, when the collection to add a new object, the first call the object's Hashcode method, the corresponding Hashcode value. In fact, in the implementation of HASHMAP will use a table to save the hashcode value of the object that has been stored in, if the table does not have the hashcode value, it can be stored directly in, no further comparison, if there is a hashcode value, The Equals method that calls it is compared with the new element, and the same is not saved, and the other addresses are hashed.
Basic rules for overriding the Hashcode () method:
· The Hashcode () method, which calls the same object multiple times during the program's run, should return the same value.
· When two objects are compared by the Equals () method to return True, the Hashcode () method of two objects returns an equal value.
· The Hashcode object is used as a standard field for the Equals () method, and should be used to calculate the value of the.
Java.lang Package "Object class"