The difference between equals and = = in Java

Source: Internet
Author: User

equals
    • If the equals method is overridden, then the equals method is used to determine whether the value of the variable of the reference data type is equal;
    • If the method is not equals overridden, then the equals method is used to determine whether the address of the object to which the reference data type refers is equal, that is, whether to point to the same object
==
    • For variables of the base data type, the comparison is whether the values stored by the variables are equal,
    • When a variable is used for a reference type, the address value of the object pointed to by the variable is equal in memory.
      First look at the following code:
Analysis

The following code:

public class EqualsDemo {    public static void main(String[] args){        int i = 3;        int j = 3;        System.out.println(i == j);// true        String str1= "hello";        String str2= "hello";        //声明了一个引用类型的变量,还没有与任何对象关联。        String str;        //通过 new String("hello") 产生一个对象(也称类 String 的实例)        //str 指向这个对象,str 即为这个对象的引用,str 存储的就是它指向的对象在内存中的地址        str = new String("hello");        String str3 = new String("hello");        String str4 = str3;        System.out.println(str.equals(str3));// true        System.out.println(str == str3);// false        System.out.println(str1.equals(str2));// true        System.out.println(str1 == str2);// true        System.out.println(str1.equals(str3));// true        System.out.println(str1 == str3);// false        System.out.println(str3.equals(str4));// true        System.out.println(str3 == str4);// true    }}

First, the == comparison operator.

i == jThe result is true, so we can understand that the value of the == variable is equal, but the str == str3 result is false.

Because there are two big data types in Java, one is the basic data type and the other is the reference data type variable. The basic data type refers byte,short,int,long,float,double,char,boolean to these 8 types of data, the reference data types are those objects, arrays of these. A String is a reference data type.

Variables of the underlying data type are stored directly in the value of the variable, so the == comparison is a direct comparison of the value of the variable, so the i == j result is true,

A variable that references a data type stores a value that is not itself, but the address value of the object it points to in memory.
STR and STR3 point to two different objects, the address values of the two objects in memory must be different, so the str == str3 result is that false;str1 and str2 point to the same object, STR3 and STR4 also point to the same object, so they compare the results to Tru E.

So you can conclude that == for variables of the basic data type, the value of the variable stored is equal, and the variable that is used for the reference type is compared to the address value of the object that the variable points to in memory.

Again,equals
All classes that inherit the Object class have the Equals method. The Euqals method of the Object class in the JDK source code is implemented like this:

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

You can see that the Equals method of object is actually == comparing two variables by operator, that is, comparing the objects that the two variables point to are equal.

But the str.equals(str3) result is true, the two variables are clearly pointing to different objects Ah, why is the result true?
The specific implementation of the Euqals method of the String class in the JDK source code:

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;    }

Oh, the original string class overrides the Equals method, which is to compare the values stored by two string variables for equality.
So the result of the comparison between the two string variables in the preceding code is true with the Equals method.

On the analysis, we can draw a summary of equals and = = at the beginning.

The difference between equals and = = in Java

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.