Detailed introduction of "equals" and "==" to get rid of the puzzlement of "==" and equals thoroughly

(1) Rules of Judgment on "=="

Basic data type, using "==" is a numerical judgment

Referring to data types, using "==" is for address determination

public static void main(String[] args) {
		int a = 3;
		int b = 3;
		//Judging the values of a and b
		System.out.println(a == b);			//true
		
		int[] arr = {1,2,3};
		int[] arr2 = {1,2,3};
		int[] arr3 = arr;

		//The variable values of arr2 correspond to the variable values of arr, but they have different addresses in the heap.
		//Ar3 refers to the address of arr
		
		System.out.println(arr == arr2);	//false
		System.out.println(arr == arr3);	//true
	}

(2) Judgment Rules on "equals"

There will be a lot about "equal".

[equals method in Object]

First, the Object object is the parent of all classes. In the Object object, it writes us an equals method.

Paste the source code:

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

It can be seen that the bottom level of the equals method in the Object class is still judged by "==". Therefore, if the subclass does not override the equals method, then the equals method is still used to determine the address. Of course, don't worry. I don't include the "String" subclass.

[equals method in String]

public boolean equals(Object anObject) {
        if (this == anObject) {
            return true;
        }
        if (anObject instanceof String) {
            String anotherString = (String)anObject;
            int n = value.length;
            if (n == anotherString.value.length) {
                char v1[] = value;
                char v2[] = anotherString.value;
                int i = 0;
                while (n-- != 0) {
                    if (v1[i] != v2[i])
                        return false;
                    i++;
                }
                return true;
            }
        }
        return false;
    }

Firstly, the addresses of two strings are judged, and if the addresses are equal, true is returned directly.

Secondly, if the addresses are different but belong to the String class, the contents of if (anObject instance of String) are carried out. Otherwise return false;

instanceof is used to determine whether the content on the left and right sides has inheritance or implementation relationships, in other words, whether the content of the actual type on the left is itself or subclass/subinterface of the content on the right.

Finally, it compares each character of the string, returns false if it does not wait, and true if it is equal.

In summary, String's equals compare values

 

[Customize classes, if judgments between values are implemented] (using the eclipse editor)

  1. First, define the attributes of the class
  2. Second, use the shortcut key (alt+shift+s) / right-click
  3. Select Generate hashCode() and equals()/(right-click version) and select source - > Generate hashCode() and equals()
  4. Select all, select ok.
public class Student {
	private int age;
	private String name;
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + age;
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Student other = (Student) obj;
		if (age != other.age)
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		return true;
	}
	
}

eclipse will help us generate equals methods and hashCode methods, which are equivalent to the equals methods of overridden parent classes.

Keywords: Eclipse

Added by mbbout on Tue, 13 Aug 2019 14:31:01 +0300