What's the difference between = = and equals?

Source: Internet
Author: User

The = = operator is specifically used to compare the values of two variables, that is, whether the value stored in the memory used to compare the variables is the same, to compare two basic types of data or two reference variables equal, only with the = = operator. (simply that the memory address of the object to which the variable is pointing is the same)


If the data that a variable points to is an object type, then this time involves two blocks of memory, the object itself occupies a chunk of memory (heap memory), and the variable occupies a chunk of memory, such as objet obj = new Object (); the variable obj is a memory, and new object () is another memory, At this point, the value stored in the memory of the variable obj corresponds to the first address of the memory that the object occupies. For variables that point to the object type, if you want to compare whether the two variables point to the same object, that is, to see if the values in memory for the two variables are equal, then you need to compare them with the = = operator.


The Equals method is used to compare the contents of two separate objects, as compared to two people whose looks are the same, compared to the two objects that are independent of each other. For example, for the following code:

String a = new string ("foo");

String b = new string ("foo");

two new statements create two objects, and then use a, B, to point to one of the objects, which is two different objects, their first address is different, that is, the values stored in a and B are not the same, so the expression a==b will return False, The contents of the two objects are the same, so the expression a.equals (b) returns True.   The comparison of strings is basically using the Equals method.


If a class does not define its own Equals method, it inherits the Equals method of the object class, and the implementation code for the Equals method of the object class is as follows:

Boolean equals (Object o) {return this==o;}

This means that if a class does not define its own Equals method, its default Equals method (inherited from the object class) is using the = = operator, and whether the object pointed to by the two variables is the same object, using equals and using = = will get the same result. If the comparison is two independent objects, the total return is false. If you are writing a class that wants to compare the contents of the two instance objects created by the class, then you must override the Equals method, and write your own code to determine at what time that the contents of the two objects are the same.

Summarize:

1. Simply say = = is the memory address of the object that the variable points to is the same

2. The memory address of the reference variable is the first address of the memory of the referenced object.

3. The Equals method is used to compare whether the contents of two separate objects are the same, if a class does not overwrite the parent class or object equals of the class, the Equals method and = = have no difference; if you overwrite the equals of the parent class or the object class, it is implemented according to our requirements, such as by the number of people, initials, and age.


equals Overwrite steps:

1) Compare two objects with = =

2) Determine if the object being compared is null

3) is not NULL, judging whether the object being compared is the same as comparing the object type or its subclasses, and making the type conversion (because it is passed in is the object type)

4) attribute Comparisons

public class Cat {private string Name;public string GetName () {return name;} public void SetName (String name) {this.name = name;} @Overridepublic boolean equals (Object obj) {if (this = = obj) return true;if (obj = = null) return false;if (!) ( obj instanceof Cat)) return false; Cat cat = (cat) obj;if (!this.getname (). Equals (Cat.getname ())) return False;return true; @Overridepublic int hashcode () {int result = This.getname (). Hashcode (); return result;}}

What's the difference between = = and equals?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.