Integer and Int

Reference blog 1

Reference blog 2

1. The difference between Integer and int

  1. Integer is a packaging class of int, int is one of the eight basic data types
  2. Integer is a class, the default value is null, int default value is 0
  3. Integer represents an object, and uses a reference to point to this object, and int is a basic data type, which directly stores values

2. Integer's automatic unboxing and boxing
Automatic unboxing and boxing are functions after jdk1.5.
Boxing is the process of encapsulating basic data types into classes, and unboxing is the opposite.
Boxing:
The object of the normal creation class is new, but the Integer class can be directly

Integer a=11;

Through the decompilation tool, we can see that the class file is like this

Integer a = Integer.valueOf(11);

11 is the basic data type, which is parsed into the Integer class.
Unboxing:

int m=a;

Decompiled class file

int m=a.intValue();

3. About "==" and equals()

In the Integer class, "==" is used to compare whether the object addresses are the same, and the Integer class overrides the equals (Object obj) method. In the equals (Object obj) method, it will first determine whether the object obj in the parameter is an Integer If it is an object of the type, it is judged whether the value is the same, the value is the same, it returns true, the value is different, it returns false, if obj is not an object of the Integer class, it returns false. It should be noted that when the parameter is the basic type int, the compiler will automatically box the int into an Integer class, and then compare.
See code

public static void main(String[] args) {
    
     
   Integer i = 10;  
   Integer j = 10;   
   System.out.println(i == j);         
   Integer a = 128;
   Integer b = 128;   
   System.out.println(a == b);        
    int k = 10;    
    System.out.println(k == i);    
    int t = 128;    
    System.out.println(t == a);         
    Integer m = new Integer(10);   
    Integer n = new Integer(10);    
    System.out.println(m == n);
}

First, directly declare Integer i = 10, it will be automatically boxed into Integer i = Integer.valueOf(10);
when k and i are compared, Integer i will be automatically unboxed as i.intValue(), and compare the two values size.

The first one is true, "==" compares addresses, because when i >= -128 and i <= 127, the first declaration will put the value of i into the cache, and the second time it will be taken directly from the cache The data inside instead of recreating an Ingeter object. Then the first print result is true because i = 10 is within the range of the cache.

The second is false, 128 is not between -128 and 127, so there is no cache when the object is first created, and a new Integer object is created the second time. So the printing result is false

The third and the fourth are all true, Integer automatically unboxes and compares the two basic data types.

The fifth one is false, which are two objects coming out of new. There is no concept of caching. Of course it is wrong to compare with ==

Guess you like

Origin blog.csdn.net/weixin_46064382/article/details/106886795