How to distinguish "==" from Equals method?

"==" generally compares the size of the basic variables (4 types and 8 types). for example:

int a=3,b=3;
System.out.println(a==b); //true
Sure, a=b. Shows true.

When comparing the contents of 2 strings for equality, for example:

String s1="HelloWorld";
String s2="HelloWorld";	
System.out.println(s1==s2);//true
Note that at this time, the things in s1 and s2 are not new, but directly using the "==" sign. In strings, the size of the string cannot be changed. (You can "slightly" treat s1 and s2 as basic variables at this time.) See the figure below:

String variables are stored in the Data Segment of the JAVA virtual machine. At this time, s1 and s2 point to the same content, namely "HelloWorld". So s1==s2. 

Next we compare objects, for example:

class Cat{
	private int height;
	String name;
	float weight;
	 Cat(String n,int h,float w){
		this.name=n;
		this.height=h;
		this.weight=w;
		}
	void run() {
			System.out.println("I am a cat, running");
		}
	
}
A new cat class is created, and the construction method has three parameters, name, height and weight.
		Cat c1=new Cat("mimi",40,15);
		Cat c2=new Cat("mimi",40,15);
		System.out.println(c1==c2);//false
		System.out.println(c1.equals(c2));//false
	
We new out two cat objects in the main program, c1 and c2, and the three parameters passed in to them are also the same. Then check if they are equal. Found that both results are false. Let's look at the first one: when "==" compares these two, it is to compare whether they are the same object. The graph in memory is shown below.

References to c1 and c2 objects are stored in the stack (it can be seen that the objects they point to are 2 objects, although the 3 member variable parameters passed in are the same, but they are always 2 things), so "c1 ==c2" is definitely not equal.

Then we see that when c1 and c2 are compared with equals ( System.out.println(c1.equals(c2));//false ), the result is still false, why is this? Let's go to the API documentation to see the description of the equals method. as follows:

Let's look at the bottom paragraph: the translation after that is: "That is, for any non-null reference values ​​x and y, the method returns true if and only if x and y refer to the same object (x == y has a value of true)." From here we know that the default implementation of the equals method in Object is the same as the comparison method of "==". So when the return value of c1==c2 is false, the return value of equals is also false, because here they compare in the same way, comparing whether c1 and c2 are the same object, the answer is of course not.

Then we are looking at the comparison of String objects, the code is as follows:

		
		String s3 =new String("HelloCSDN");
		String s4 =new String("HelloCSDN");
		System.out.println(s3==s4); //false
		System.out.println(s3.equals(s4));//true
At this time, s3 and s4 are new. They all pass in the same parameter "HelloCSDN". When comparing with the "==" sign, the result is found to be false, but after the comparison with equals, the result is found to be true. Why is this? s3 and s4 are two objects of the String class, so we went to check the part of the API about the methods contained in the String class, and found that it also has the equals method, indicating that the equals method has been rewritten in the String class. The contents are as follows:

Compares this string to the specified object. The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object.

Translation: Compares this string with the specified object. The result is true if and only if the argument is not null and is a string object representing the same sequence of characters for this object.

At this time, the equals method called when s3 and s4 are compared is the rewritten method. According to the content of the above method, they compare the "character sequence". The two parameters we pass in are "HelloCSDN", so s3, s4 , the result after equals is true.

Here we rewrite the equals method in the Cat class, and want it to be compared according to our comparison method, (even if the two cats are not the same object, but their names, heights, and weights are the same, we consider them to be the same Object). The rewritten code is as follows:

public boolean equals(Object obj) {
		if(obj==null) return false;
		else {
			if(obj instanceof Cat) {
				Cat c= (Cat)obj;
				if(c.name==this.name&&c.height==this.height&&c.weight==this.weight) {
					return true;
				}
			}
		}
		return false;
	}
At this point we are running the program
		Cat c1=new Cat("mimi",40,15);
		Cat c2=new Cat("mimi",40,15);
		System.out.println(c1==c2);//false
		System.out.println(c1.equals(c2));//true
It is found that c1 and c2 are compared with equals at this time, and the result is true, which is in line with our wishes.


When comparing two objects in practice, if you want to call the equals method, you must first find out whether the equals method is overridden in the classes of the two objects to be compared (check the API documentation). If not, you need to manually reset it yourself. Call it after writing. Otherwise the result is the same as the result of "==". . .

(When I was first learning JAVA, the problem of "==" "equals" bothered me for a long time. After watching the JAVA teaching video of Mr. Ma Soldier, I finally understood)~


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325626335&siteId=291194637