Object Based on Java
Object is the final ancestor of all classes ., You can use the Object class to reference any type of objects. In Java, only the basic type is not an object.
Equals Method
The equals method in the Object class is used to check whether an Object is equal to another Object. In the Object class, this method determines whether two objects have the same reference.
However, in the general sense, equality not only has the same reference, but if the State of the other object is equal, it is considered that the other object is equal. Therefore, the subclass must override the equals method in the super class.
In Java, the equals method is required to have the following features:
- Assertion. x. equals (x) returns true. Symmetry. x. equals (y) returns the same value as y. equals (x. Passed, for any reference x, y, z, if x. equals (y) returns true, y. equals (z) returns true, then x. equals (z) also returns true. Consistency. If the objects referenced by x and y do not change, the same result should be returned when x. equals (y) is repeatedly called. For any non-null reference x, x. euqals (null) should return false. Two equals methods are implemented as follows:[Java]View plaincopy
- Package com. xujin;
-
- Import java. util. Arrays;
-
- Public class Test {
- Public static void main (String [] args ){
- Employee employee = new Employee ("Bod", 5000 );
- Employee staff = employee;
- Manager manager = new Manager ("Bod", 8000,600 0 );
- System. out. println (employee. equals (new Employee ("Bod", 5000); // true
- System. out. println (employee. equals (staff); // true
- System. out. println (employee. equals (null); // false
- System. out. println (manager. equals (new Manager ("Bod", 8000,600 0); // true
- System. out. println (employee. equals (manager); // false
-
- System. out. println (employee instanceof Employee); // true
- System. out. println (employee instanceof Manager); // false
- System. out. println (manager instanceof Employee); // true
-
-
- // Check whether the two arrays are the same
- System. out. println (Arrays. equals (new int [] {1, 2, 3}, new int [] {1, 2, 3}); // true
- }
- }
-
- Class Employee {
- Public Employee (String name, double salary ){
- This. name = name;
- This. salary = salary;
- }
-
- Public boolean equals (Object otherObject ){
- // Returns true if the two objects have the same reference.
- If (this = otherObject)
- Return true;
- // If otherObjext is null, false is returned.
- If (otherObject = null)
- Return false;
- // If the classes of the two objects are different, false is returned.
- If (this. getClass ()! = OtherObject. getClass ())
- Return false;
-
- // At this time, the two objects are neither null nor belong to the same class.
- Employee other = (Employee) otherObject;
- Return name. equals (other. name) & salary = other. salary;
-
- }
-
- // Define variables
- Private double salary;
- Private String name;
- }
-
- Class Manager extends Employee {
- Public Manager (String name, double salary, double bonus ){
- Super (name, salary );
- This. bonus = bonus;
- }
-
- Public boolean equals (Object otherObject ){
- If (! Super. equals (otherObject ))
- Return false;
- Manager other = (Manager) otherObject;
- Return bonus = other. bonus;
- }
-
- Private double bonus;
- }
Is the HashCode method hashed? It is an integer value exported from an object. This method is defined by an Object and returns the storage address of the Object. Therefore, if necessary, the subclass must overwrite the HashCode method. Note: If you override the equals method, you must override the HashCode method, because the two methods must be consistent: If the equals method returns true, the HashCode method must return the same value.
In the following example, the String class has overwritten the HashCode method and used the object value to export the hash code. The StringBuilder method does not overwrite the HashCode method and returns the address of the storage unit.
[Java]View plaincopy
- Package com. xujin;
-
- Public class Main {
- Public static void main (String... args ){
- String s1 = "Hello, world! ";
- String s2 = new String ("Hello, world! ");
- System. out. println (s1.hashCode (); // 2007142665
- System. out. println (s2.hashCode (); // 2007142665
-
- StringBuilder sb1 = new StringBuilder ("Hello, world! ");
- StringBuilder sb2 = new StringBuilder (s1 );
- System. out. println (sb1.hashCode (); // 1291383602
- System. out. println (sb2.hashCode (); // 1814462232
- }
- }
The toString method is used in the Object class to return the class name and hash code of the Object. For example[Java]View plaincopy
- Package com. xujin;
-
- Public class Main {
- Public static void main (String... args ){
- System. out. println (System. out); // java. io. PrintStream @ 6102d81c
- }
- }
The output is a java. io. PrintStream @ 6102d81c, because the PrintStream class of System. out does not overwrite the toString method of the Object.
In most cases, it is used to return a string that represents the object value. Most toString methods follow this format: class name [Domain value, Domain value, Domain value ......]
For example:[Java]View plaincopy
- Package com. xujin;
-
- Public class Main {
- Public static void main (String... args ){
- Employee staff = new Employee ("Bob", 8000 );
- System. out. println (staff. toString ());
- }
- }
-
- Class Employee {
- Public Employee (String name, double salary ){
- This. name = name;
- This. salary = salary;
- }
-
- Public String toString (){
- Return getClass (). getName () + "[name =" + name + ", salary =" + salary + "]";
- }
-
- // Define variables
- Private double salary;
- Private String name;
- }