== operator on objects in Java

I cannot understand the difference in how the obj and obj2 objects are created in the following code. In particular, I'm not sure how the primitive is dropped on an object. Considering some other issues, I thought it was impossible. But the following program compiles and works fine. In the first case, the conclusion will be false, and in the second case this is true.

public class Test {

    public static void main(String args[]){

        Integer num = new Integer(3) ;
        Object obj = num;
        Integer[] integerArr = {1, 2, 3, 4};
        Object[] objArr = integerArr;
        boolean contains = false;

        for (int i = 0; i < objArr.length; i++){
            if (objArr[i] == obj){
                contains = true;
                break;
            }
        }

        System.out.println(contains);

        int num2 = 3 ;
        Object obj2 = num2;
        Integer[] integerArr2 = {1, 2, 3, 4};
        Object[] objArr2 = integerArr2;
        boolean contains2 = false;

        for (int i = 0; i < objArr2.length; i++){
            if (objArr2[i] == obj2){
                contains2 = true;
                break;
            }
        }

        System.out.println(contains2);
    }

}
+5
source share
4 answers

You must understand this method from java.lang.Integer

public static Integer valueOf(int i) {
    assert IntegerCache.high >= 127;
    if (i >= IntegerCache.low && i <= IntegerCache.high)
        return IntegerCache.cache[i + (-IntegerCache.low)];
    return new Integer(i);
}

In the first case, you create a new object Integer, and in the second case, the compiler performs the conversion for you using the cache.

/**
 * Cache to support the object identity semantics of autoboxing for values between
 * -128 and 127 (inclusive) as required by JLS.
 *
 * The cache is initialized on first usage.  The size of the cache
 * may be controlled by the -XX:AutoBoxCacheMax=<size> option.
 * During VM initialization, java.lang.Integer.IntegerCache.high property
 * may be set and saved in the private system properties in the
 * sun.misc.VM class.
 */

Here is the corresponding bytecode to find out how it ends with a call to the constructor Integeror Integer.valueOf:

0: new #2; //class java/lang/Integer
3: dup
4: iconst_3
5: invokespecial #3; //Method java/lang/Integer."<init>":(I)V
8: astore_1
9: aload_1
10: astore_2
11: iconst_4
12: anewarray #2; //class java/lang/Integer
15: dup
16: iconst_0
17: iconst_1
18: invokestatic #4; //Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
21: aastore
...
90: iconst_3
91: istore 6
93: iload 6
95: invokestatic #4; //Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
98: astore 7
100: iconst_4
101: anewarray #2; //class java/lang/Integer
104: dup
105: iconst_0
106: iconst_1
107: invokestatic #4; //Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
110: aastore
111: dup
...
+3
source

== ( ), equals() ( ).

. , , , , Integer ( -127 127, ), , .

, true:

Integer a = 127;
Integer b = 127;
a == b // true

false!

Integer a = 128;
Integer b = 128;
a == b // false

: equals() .

+4

== object1 object2 , == , object1 object2 .

equals object1.equals(object2),

:

String.equals(String other) , ==.

, == , .

+1

int object Integer. JVM "" Integer . , .

In the first case, you always compare the object with another object, and in your example they will never be equal.

0
source

All Articles