[JAVA Basics] = = difference from equals

==Introduction

Its function is to judge whether the addresses of two objects are equal. That is, to determine whether two objects are the same object (basic data typeCompare values, reference data typesCompare memory addresses).

  • Basic data types : byte, short, char, int, long, float, double, boolean. The comparison between them, using the double equal sign (==), compares their values.
  • Reference data types : When they are compared with (==), they compare their storage addresses in memory (to be precise, heap memory addresses).

for example:

public static void main(String[] args) {
    
    

    int i = 100;//基本数据类型
    int ii = 100;//基本数据类型
    Integer j = 100;//引用类型
    Integer jj = 100;//引用类型
    Integer k = new Integer(100);//引用类型
    Integer kk = new Integer(100);//引用类型
    System.out.println("i的地址:" + System.identityHashCode(i));
    System.out.println("ii的地址:" + System.identityHashCode(ii));
    System.out.println("j的地址:" + System.identityHashCode(j));
    System.out.println("jj的地址:" + System.identityHashCode(jj));
    System.out.println("k的地址:" + System.identityHashCode(k));
    System.out.println("kk的地址:" + System.identityHashCode(kk));

    //基本类型相互比较其中的值,所以得出true
    System.out.println("i == ii 结果:" + (i == ii));
    //当int的引用类型Integer与基本类型进行比较的时候,包装类会先进行自动拆箱
    //然后与基本类型进行值比较,所有得出true
    System.out.println("i == j 结果:" + (i == j));
    //同上,包装类先拆箱成基本类型,然后比较,得出true
    System.out.println("i == k 结果:" + (i == k));
    //都是引用类型,所有比较的是地址,因为j与jj的地址相同,得出true
    System.out.println("j == jj 结果:" + (j == jj));
    //都是引用类型,所有比较的是地址,因为k与kk的地址不相同,得出false
    System.out.println("k == kk 结果:" + (k == kk));
}

Output result:

i的地址:713338599
ii的地址:713338599
j的地址:713338599
jj的地址:713338599
k的地址:168423058
kk的地址:821270929
i == ii 结果:true
i == j 结果:true
i == k 结果:true
j == jj 结果:true
k == kk 结果:false

Doubt: Why are the addresses of j and jj the same, but the addresses of k and kk are different?

Answer: For parameters with an Integer value of -128~127 and assigned in the form of Integer x = value;, x will be automatically unboxed from the packaging type to a basic data type for reuse! So the memory addresses of j and jj are the same!

Let's turn 100 into 1000 and try!

public static void main(String[] args) {
    
    

    int i = 1000;//基本数据类型
    int ii = 1000;//基本数据类型
    Integer j = 1000;//引用类型
    Integer jj = 1000;//引用类型
    Integer k = new Integer(1000);//引用类型
    Integer kk = new Integer(1000);//引用类型
    System.out.println("i的地址:" + System.identityHashCode(i));
    System.out.println("ii的地址:" + System.identityHashCode(ii));
    System.out.println("j的地址:" + System.identityHashCode(j));
    System.out.println("jj的地址:" + System.identityHashCode(jj));
    System.out.println("k的地址:" + System.identityHashCode(k));
    System.out.println("kk的地址:" + System.identityHashCode(kk));

    //基本类型相互比较其中的值,所以得出true
    System.out.println("i == ii 结果:" + (i == ii));
    //当int的引用类型Integer与基本类型进行比较的时候,包装类会先进行自动拆箱
    //然后与基本类型进行值比较,所有得出true
    System.out.println("i == j 结果:" + (i == j));
    //同上,包装类先拆箱成基本类型,然后比较,得出true
    System.out.println("i == k 结果:" + (i == k));
    //都是引用类型,所有比较的是地址,因为j与jj的地址不相同,得出false
    System.out.println("j == jj 结果:" + (j == jj));
    //都是引用类型,所有比较的是地址,因为k与kk的地址不相同,得出false
    System.out.println("k == kk 结果:" + (k == kk));
}

Output result:

i的地址:713338599
ii的地址:168423058
j的地址:821270929
jj的地址:1160460865
k的地址:1247233941
kk的地址:258952499
i == ii 结果:true
i == j 结果:true
i == k 结果:true
j == jj 结果:false
k == kk 结果:false

When j and jj exceed the range of -128~127, the address changes, so the result of the comparison is false. Let’s look at the automatic unboxing of other wrappers:

type describe
Boolean All automatic unboxing
Byte All automatic unboxing
Short -128~127 interval automatic unboxing
Integer -128~127 interval automatic unboxing
Long -128~127 interval automatic unboxing
Float no unboxing
Doulbe no unboxing
Character 0~127 interval automatic unboxing

Introduction to equals() method

Its function is also to judge whether two objects are equal. But it generally has two use cases:

  • Case 1: The class does not override the equals() method. Then when comparing two objects of this class through equals(), it is equivalent to comparing these two objects through "==".
  • Case 2: The class overrides the equals() method. Generally, we override the equals() method to compare whether the contents of two objects are equal; if their contents are equal, return true (that is, consider the two objects equal).

The wrapper classes of the 8 basic types of Boolean, Byte, Short, Integer, Long, Float, Doulbe, and Character all rewrite the equals() method, so when comparing, if the contents are the same, return true, for example:

//因为内容相同,返回的都是true
System.out.println("j.equals(jj) 结果:" + (j.equals(jj)));
System.out.println("(k.equals(kk) 结果:" + (k.equals(kk)));

Introduction to the comparison of String type

String is a very special data type, it can be assigned by String x = value;, or it can be assigned by String x = new String(value).

String x = value; parameters will be placed in the constant pool memory block area; String x = new String(value) parameters will be placed in the heap memory area and treated as objects.

for example:

public static void main(String[] args) {
    
    
    String a = new String("ab"); // a 为一个引用
    String b = new String("ab"); // b为另一个引用,对象的内容一样
    String aa = "ab"; // 放在常量池中
    String bb = "ab"; // 从常量池中查找
    System.out.println("a地址:" + System.identityHashCode(a));
    System.out.println("b地址:" + System.identityHashCode(b));
    System.out.println("aa地址:" + System.identityHashCode(aa));
    System.out.println("bb地址:" + System.identityHashCode(bb));
    //地址相同,所以返回true
    if (aa == bb) {
    
    
        System.out.println("aa==bb");
    }
    // 地址不同,非同一个对象,所以返回false
    if (a == b) {
    
    
        System.out.println("a==b");
    }
    //地址不同,但是内容相同,所以返回true
    if (a.equals(b)) {
    
    
        System.out.println("aEQb");
    }
}

Output result:

a地址:713338599
b地址:168423058
aa地址:821270929
bb地址:821270929
aa==bb
aEQb

Why the equals() method of string returns true, because string rewrites the equals() method, the source code is as follows:

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

Returns true if the content is the same!

Summarize

If you need to compare whether an object is the same, you must rewrite equals() to compare whether the contents are the same. If they are the same, return true; otherwise, return false!

Guess you like

Origin blog.csdn.net/jiang_wang01/article/details/131414768