1. The object class is the base class for all Java classes
If the base class is not indicated in the declaration of the class with the extends keyword, the default base class is the object class, ex:
public class person{
~~~~~
}
Equivalent to
public class Person extends object{
~~~~~
}
2. The Equals method of the object class
①, the object class is defined as:
public boolean equals (Object obj) method.
Provides logic to define whether objects are equal.
The Equals method of ② and OBJEC is defined as: X.equals (y) returns True when X and Y are applied to the same object, otherwise false.
Some of the classes provided by ③, J2SDK, such as String,date, override the Equals () method of object, invoke the Equals method of these classes, X.equals (y), when X and Y refer to the same class of objects, and the property content is equal (not necessarily an equal object) , returns True otherwise returns false.
④, you can override the Equals () method in the user's custom type as needed.
Public classtestequals{ Public Static voidMain (String args[]) {Cat CAT1=NewCat (All-in-); Cat Cat2=NewCat (All-in-); System.out.println (CAT1==cat2); System.out.println (Cat1.equals (CAT2)); String S1=NewString ("Hello"); String S2=NewString ("Hello"); System.out.println (S1==S2); System.out.println (s1.equals (S2)); }}classcat{intcolor; intHeight,weight; Cat (intColor,intHeight,intweight) { This. color=color; This. Height =height; This. Weight =weight; } Public Booleanequals (Object obj) {if(obj = =NULL)return false; Else{ if(objinstanceofCat) {Cat C=(Cat) obj; if(C.color = = This. color && C.height = = This. height && C.weight = = This. Weight) { return true; } } } return false; }}
Operation Result:
Introduction to the object class in Java