Difference between int and Integer:

1, int is a basic data type, stores a value, default is 0

2. An Integer is a wrapper for an int that contains a reference to an object and must be instantiated before being used. The default value is NULL

Int and Integer

== Address comparison

ValueOf (int) ¶ If an int is assigned to an Integer, an int will be boated into an Integer. Integer.valueof (int) is called. If the value range is -128 to 127, the object will be fetched from the constant pool by default, referencing the same address. Out of range new objects, reallocating memory in the heap.

2. When an int is compared with an Integer, the unpacking operation is automatically performed on the Integer.

3, all new objects are allocated in the heap, reference address must be different.

Equals value comparison

In object.java, equals is actually implemented as the address comparison ==, Integer overrides equals() and compares the same value.

  • Integer.valueof () Specifies the share mode. An instance is shared between -128 and 127.
public static Integer valueOf(int i) {
    if (i >= IntegerCache.low && i <= IntegerCache.high)
        return IntegerCache.cache[i + (-IntegerCache.low)];
    return new Integer(i);
}
Copy the code
  • Integer.equals()
public boolean equals(Object obj) {
    if (obj instanceof Integer) {
        return value == ((Integer)obj).intValue();
    }
    return false;
}
Copy the code

Integer.class

public final class Integer extends Number implements Comparable<Integer> { private final int value; // Integer contains the value}Copy the code

== equals

Tests, results are marked below

public static void compareInt2Integer() {/** ** int (128) {/** * int (128);true
     */
    Integer i = 127;
    Integer j = 127;
    System.out.println(i == j);    // true
    System.out.println(i.equals(j)); // true/** ** m and n are newly created objects, and memory will be reallocated in the heap.false
     */
    Integer m = 128;
    Integer n = 128;
    System.out.println(m == n);   //false
    System.out.println(m.equals(n)); //true/** ** k = cache, h = newly created object,false
     */
    Integer k = 127;
    Integer h = new Integer(127);
    System.out.println(k == h);   //falseSystem.out.println(k.equals(h)); //true/** * new allocates memory. A and b are newly created objects * ==false
     */
    Integer a = new Integer(127);
    Integer b = new Integer(127);
    System.out.println(a == b);  //false
    System.out.println(a.equals(b)); //true/** ** integer.valueof () takes priority over the constant pool cache, which ranges from -128 to 127. If the cache exceeds the range, an object will be createdfalse
     */
    Integer w = 128;
    Integer y = Integer.valueOf(128);
    System.out.println(w == y); // false
    System.out.println(w.equals(y)); //true/ * * * int and an Integer comparison, the Integer automatically split open a case to int, numerical comparison,true
     */
    Integer x = 128;
    int z = 128;
    System.out.println(x == z); //true
    System.out.println(x.equals(z)); //true
}
Copy the code