I'm your ancestor

Keywords: Java

;

Object class 😊

Is the parent of all classes. (all classes inherit directly or indirectly from the Object class)

Its parameters can receive any object, and it can return any object as a return value.

Refer to the tutorial: (/▽\)

toString method

It is recommended to override the toString method, otherwise the address will be printed.

When outputting a reference, println() will automatically call the toString() method of the reference.

Source code:

public String toString() {
return this.getClass().getName() + "@" + Integer.toHexString(hashCode());
}

Default implementation method: convert the memory address of the class name @ object to hexadecimal

Case:

// Define MyTime class
class MyTime{
	int year;
	int month;
	int day;
	public MyTime(){}
	public MyTime(int year, int month, int day){
		this.year = year;
		this.month = month;
		this.day = day;
	}
	public String toString(){
		return this.year + "/" + this.month + "/" + this.day;
	}
}
// Test class
public class Test01{
	public static void main(String[] args){
		MyTime t1 = new MyTime(1970, 1, 1);
		// If a date object is converted into a string, I may still want to see the specific date information.

		//Before the MyTime class overrides the toString() method
		//System.out.println(t1.toString()); // MyTime@28a418fc
		
		//After the MyTime class overrides the toString() method
		System.out.println(t1.toString()); // 1970/1/1

	}
}

equals method

Use the equals method to determine whether two objects are equal.

In the equals method in the Object class, the default is "= =" to judge whether two java objects are equal. And "= =" determines the memory addresses of two java objects. Therefore, the default equals method determines the memory address of the Object.

Note the null pointer exception: this cannot be equal to null

Source code:

public boolean equals(Object obj) {
return (this == obj);
}

Case:

// Still define the MyTime class, but override the equals method
public boolean equals(Object obj) {
	if(obj == null || !(obj instanceof MyTime)){
		return false;
	}
	if(this == obj){
		return true;
	}
	MyTime t = (MyTime)obj;
	return this.year == t.year && this.month == t.month && this.day == t.day ;
}
// Main program of test class
public static void main(String[] args){
	// To judge whether the data of two basic data types are equal, just use "= =". "= =" judge whether 100 saved in a is equal to 100 saved in b.
	int a = 100;
	int b = 100;
	System.out.println(a == b); //true (equal) false (unequal)
	
	// Create a date object: August 8, 2008.
	MyTime t1 = new MyTime(2008, 8, 8); //MyTime t1 = 0x1234;
	// A new date object is created, but the date represented is also August 8, 2008.
	MyTime t2 = new MyTime(2008, 8, 8); //MyTime t2 = 0x3698;
	
	// The "= =" here determines whether the object memory address saved in t1 and the object memory address saved in t2 are correct
	System.out.println(t1 == t2); // false
	
	// Before overriding the Object equals method (the object memory address is compared)
	System.out.println(t1.equals(t2)); //false
	
	// After overriding the Object equals method (the content is compared.)
	boolean flag = t1.equals(t2);
	System.out.println(flag); //true

	MyTime t4 = null;
	System.out.println(t1.equals(t4)); //false
}

Summary:

  • Whether the basic data types in java are equal, use==
  • All reference data types in java use the equals method to determine whether they are equal.

equals() override considerations

Step 1: first design the Address class

class Address{
	String city;
	String street;
	String zipcode;

	public Address(){}
	public Address(String city,String street,String zipcode){
		this.city = city;
		this.street = street;
		this.zipcode = zipcode;
	}

	// The equals method here determines whether the Address object and the Address object are equal.
	public boolean equals(Object obj){
		if(obj == null || !(obj instanceof Address)) return false;
		if(this == obj) return true;
		// The same city, the same street and the same zip code mean that the same family address is the same.
		Address a = (Address)obj;
		if(this.city.equals(a.city) 
			&& this.street.equals(a.street) 
			&& this.zipcode.equals(a.zipcode)){
			return true;
		}
		return false;
	}
}

Step 2: redesign the User class

class User{
	String name; 	// user name
	Address addr;	// User's address

	public User(){}
	public User(String name, Address addr){
		this.name = name;
		this.addr = addr;
	}

	// Override the equals method
	// Rewrite rule: when a user's user name and home address are the same, it means the same user.
	// This equals determines whether the User object is equal to the User object.
	public boolean equals(Object obj){
		// When the user name is the same as the user name, and the address is the same as the address, it is considered to be the same user.
		if(obj == null || !(obj instanceof User)) return false;
		if(this == obj) return true;
		
		User u = (User)obj;
		if(this.name.equals(u.name) && this.addr.equals(u.addr)){
			return true;
		}
		return false;
	}
}

Step 3: Test Procedure

public static void main(String[] args){
	User u1 = new User("zhangsan", new Address("Beijing","Daxing District","11111"));
	User u2 = new User("zhangsan", new Address("Beijing","Daxing District","11111"));
	System.out.println(u1.equals(u2)); // true
	
	User u3 = new User("zhangsan", new Address("Beijing","Chaoyang District","11112"));
	System.out.println(u1.equals(u3)); // false
}

equals and toString in the String class

The String class has overridden the equals method. You cannot use = =, but must use equals when comparing two strings.
The String class has overridden the toString method.

hashCode method

The hashCode() method returns a hash code, which can be regarded as the memory address of a java object.

(in fact, it is the memory address of a java object and a value obtained through the hash algorithm.)

Source code:

public native int hashCode();

This method is not an abstract method. It has a native keyword and calls the C + + program at the bottom.

// Define MyClass class
class MyClass{}
// Main method of test program
public static void main(String[] args){
	Object o = new Object();
	int hashCodeValue = o.hashCode();
	System.out.println(hashCodeValue); //798154996

	MyClass mc = new MyClass();
	int hashCodeValue2 = mc.hashCode();
	System.out.println(hashCodeValue2); //1392838282

}

getClass method

Returns the actual object type stored in the reference.

Application: it is usually used to judge whether the actual storage object types in two references are consistent.

// Define MyClass class
class MyClass{}
// Main method of test program
public static void main(String[] args) {
	MyClass mc1 = new MyClass();
	MyClass mc2 = new MyClass();
	System.out.println(mc1.getClass() == mc2.getClass()) // true
}

Clone method (understand)

Refer to: (/▽\)

Knowledge points involved: deep copy and shallow copy

finalize method (goodbye)

Has withdrawn from the stage of history.

Posted by infomamun on Sat, 11 Sep 2021 16:00:56 -0700