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 (1) Language overview
Java know how much (2) virtual machine (JVM) and cross-platform principle
Java know how much (3) employment direction
Java know how much (4) the difference between J2SE, Java EE, J2ME
Java know how much (5) Build Java development environment
Java know how much (6) The first example of a program
Java knows how many (7) classes and objects
Java know how much (8) class library and its organizational structure
Java know how much (9) Import and Java class search path
Java know how much (10) data types and variables
Java know how much (11) data type conversions
Java know how many (12) operators
Java know how much (13) Process Control
Java know how many (14) arrays
Java know how much (15) string
Java know how much (StringBuffer) and Stringbuider
Java know how much (17) emphasize the programming style
Java know how many (18) classes are defined and instantiated
Java know how many (19) access modifiers (access control characters)
Java knows how many (20) variables are scoped
Java know how much (+) This keyword is detailed
Java know how many (22) method overloads
Java know how much (23) the basic run order of classes
Java know how much (24) packaging class, unpacking and packing detailed
Java know how much (25) More about Java package
Java know how much (26) claim rules for source files
Java know how much (27) the concept and implementation of inheritance
Java know how many super keywords
Java know how much (29) Overwrite and reload
Java know how much (30) polymorphic and dynamic binding
Java know how many static keywords and Java static variables and static methods
Java know how much (instanceof)
Java know how much (33) type conversions for polymorphic objects
Java know how much (final keyword): Block inheritance and polymorphism
Java knows how many () object classes
Java knows how many () object classes