The Object class is in the Java.lang package and is the ancestor of all Java classes, and each class in Java is extended by it.
When defining a Java class, if no indicated parent class is displayed, the Object class is inherited by default. For example:
1 Public class demo{2 // ... 3 }
is actually shorthand for the following code:
1 Public class extends object{2 // ... 3 }
In Java, only the base type is not an object, such as numeric, character, and Boolean values are not objects, all array types, whether an object array or an array of primitive types, are inherited from the object class.
The Object class defines some useful methods, because they are root classes, which exist in other classes and are generally overloaded or overwritten to achieve their specific functions.
Equals () method
The Equals () method in the object class is used to detect whether an object is equivalent to another object, and the syntax is:
public boolean equals (Object obj)
For example:
1 obj1.equals (OBJ2);
In Java, the basic meaning of data equivalence is that the value of two data is equal. When comparing by Equals () and "= =", reference type data compares references, that is, memory addresses, and the base data type compares values.
Attention:
- The Equals () method can only compare reference types and "= =" to compare reference types and basic types.
- When comparing with the Equals () method, the class File, String, Date, and wrapper classes are comparison types and content regardless of whether the reference is the same instance.
- When you compare with "= =", the data types on both sides of the symbol must be the same (except for data types that can be automatically converted), otherwise the compilation will fail, and the two data compared with the Equals method is only a reference type.
Hashcode () method
The hash code (HASHCODE) is a numerical value obtained by the object according to certain algorithm, and the hash code has no regularity. If x and Y are different objects, X.hashcode () is basically not the same as Y.hashcode ().
The Hashcode () method is primarily used to implement operations such as Quick Find in a collection, or to compare objects.
In Java, the rules for Hashcode are as follows:
- During the same application execution, calling Hashcode () on the same object must return the same integer result-provided that the information compared by equals () has not been altered. There is no need to agree on the results of the same application's invocation at different execution periods.
- If two objects are considered equal by the Equals () method, calling Hashcode () on both objects must obtain the same integer result.
- If two objects are considered unequal by the Equals () method, calling Hashcode () on both objects does not have to produce different integer results. However, programmers should be aware that producing different integer results for different objects is likely to improve the efficiency of Hashtable (a class in the collection framework, which is later learned).
Simply put: If two objects are the same, their hashcode values must be the same, and if two objects have the same hashcode values, they are not necessarily the same. In the Java specification, it is generally overridden that the Equals () method should overlap the hashcode () method.
ToString () method
The ToString () method is another important method defined in the object class, which is the string representation of the objects, and the syntax is:
Public String toString ()
The return value is a String type that describes the information about the current object. The ToString () method implemented in the object class returns the type and memory address information for the current object, but overrides it in some subclasses (such as String, Date, and so on), or optionally overrides the ToString () method in a user-defined type to return more appropriate information.
In addition to the ToString () method of an explicitly called object, the ToString () method is called automatically when a String is connected to another type of data.
The above methods are often used in Java, here is a brief introduction to the object class and other classes to understand, detailed instructions please refer to the Java API documentation.
Series Articles:Java know how much (top)Java know how much (interface) interfaceJava knows how much (40) the difference between an interface and an abstract classJava know how much (41) generic explanationJava know how much (42) the range of generic wildcard characters and type parametersJava know how much (43) Exception Handling BasicsJava know how much (44) exception typeJava know how much (45) uncaught ExceptionsHow much Java knows (the) use of try and catchJava know how much (47) use of multiple catch statementsJava knows how much (in) the nesting of try statementsJava know how much (a) Throw: Exception throwsJava know how many () Java throws clausesJava knows how many (or) finallyJava know how much (52) built-in exceptionsJava know how much (53) Use Java to create your own exception subclassesJava know how much (54) assertion detailedJava know how many (55) threadsJava know how much (56) threading ModelJava know how much (57) main threadJava know how much (58) thread Runnable interface and thread class explanationJava know how much (59) Create multithreadingJava know how much (isAlive) and join () useJava know how much (61) thread PriorityJava know how much (62) thread synchronizationJava know how much (63) inter-thread communicationJava know how much (64) thread deadlockJava know how much (65) thread hangs, resumes, and terminatesJava know how much (66) Overview of input and output (IO) and streamsJava know how much (67) character-oriented input streamJava know how much (68) character-oriented output streamJava know how much (69) byte-oriented input and output streamJava know how much (70) application for byte streamJava know how much (71) file and directory managementJava know how much (72) random Read and write filesJava know how much (73) file compression processingJava know how much (74) base Class library
Java know how much (the) object class