“This is the 13th day of my participation in the August More Text Challenge. For details, see: August More Text Challenge.”

Object class

The Object class is located under the java.lang package.

Class Object is the root class of the class hierarchy. Each class uses Object as its superclass (superclass). All objects (including arrays) implement the methods of this class.

Each class inherits the Object class by default.

The toString () method

Public String toString() Returns a String representation of the object. In general, the toString method returns a string that "represents" the object as text. The result should be a concise but easy-to-read information expression. It is recommended that all subclasses override this method. The toString method of the Object class returns a string consisting of the name of the class (of which the Object is an instance), the at token "@", and an unsigned hexadecimal representation of the Object's hash code. In other words, this method returns a string whose value is equal to: getClass().getName() + '@' + INTEger.tohexString (hashCode())Copy the code
People people = new People();
String s = people.toString();
System.out.println(s);
Copy the code

As you can see, if the toString method of the Object class is not overridden, the result is the class name + “@” + hexadecimal representation of the Object’s hash code.

In practice, however, this address value is of no practical significance to us, so we generally override this method as we see fit.

The equals () method

Public Boolean equals(Object obj) indicates whether some other Object is "equal" to this Object.Copy the code

The equals method implements equality on non-empty object references:

  • Reflexivity: x.equals(x) should return true for any non-null reference value x.

  • Symmetry: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.

  • Transitivity: for any non-null reference values x, y, and z, if x.equals(y) returns true, and y.equals(z) returns true, then x.equals(z) should return true.

  • Consistency: Multiple calls to x.equals(y) always return true or false for any non-null reference values x and y, provided that the information used in the equals comparison on the object has not been modified.

X.equals (null) should return false for any non-null reference value x.

The equals method of the Object class implements the highest possible difference between objects. That is, for any non-null reference values x and y, this method returns true if and only if x and y refer to the same object (x == y has the value true).

Note: When this method is overridden, it is often necessary to override the hashCode method to maintain the general convention of hashCode methods, which states that equal objects must have equal hash codes.

Write in the last

The toString method and equals method are commonly used and should be well understood.

The above is the use of the two methods and some easy wrong points, if there are not right, welcome to dig friends to criticize and correct.