Java - summary of difference between int and Integer

Before talking about the difference between int and Integer, two questions must be clarified:

1. Java basic data types and their encapsulation classes

Two, java automatic unboxing and automatic boxing

1. Java basic data types and their encapsulation classes

type of data byte size Package class
byte 8 bits Byte
short 16 bits Short
int 32 bit Integer
long 64 bit Long
float 32 bit Float
double 64 bit Double
boolean 1 person Boolean

Java data types include basic data types and reference data types. In order to facilitate the processing of basic data types as objects, java introduces the corresponding encapsulation classes for basic data types. For example, the int encapsulation class is Integer 

Two, java automatic unboxing and automatic boxing 

 1. Automatic packing

Autoboxing is actually converting basic data types to reference data types (objects)

2. Automatic unboxing

Automatic unboxing is actually converting reference data types to basic data types

code show as below:

    public static void main(String[] args) {
        Integer a = 1;//这里就用到了自动装箱;等同于Integer a = new Integer(1);

        int b = a - 1;//对象不能直接进行计算,所以这里有自动拆箱的操作,将a对象转换成基本数据类型,然后-1
        System.out.println(a);
        System.out.println(b);
    }

 Output result:

1
0

3. The difference between int and Integer

From the above we can see the difference between int and Integer:

  • int is a basic data type, and Integer is a reference data type;
  • The default value of int is 0, and the default value of Integer is null;
  • The int type directly stores the value, and the Integer needs to instantiate the object and point to the address of the object.

 Speaking of this, do you think that’s all there is to it? In fact, there are some differences in details between them, as follows:

    public static void main(String[] args) {
        Integer a = new Integer(1);
        Integer b = new Integer(1);

        int c = 1;
        int d = 1;

        Integer e = 1;
        Integer f = 1;
        
        Integer g = 130;
        Integer h = 130;
        
        Integer i = new Integer(130);
        int j = 130;
    }
  • 1: Does a == b? Nonsense, certainly not equal. The addresses of the two new objects are different.
  • 2: Is c == d? This is also nonsense, the values ​​​​of all basic data types must be equal.
  • 3: The key question now is does  e == f  ? g == h  ? 

        The answer is:  e == f; g != h. Why does this happen? Because Integer g = 130 will be compiled into Integer.ValueOf(130) when Java is compiling , this can be seen by decompiling the class file. From the Integer source code, it can be concluded that the Integer.ValueOf() method will cache the Integer between the value -128~127, and will not renew one, so e==f; when the value is greater than 127 or less than -128 Sometimes it will be a new one, so g!=h.

The ValueOf() method of Integer is as follows:

    public static Integer valueOf(int i) {
         //IntegerCache.low == -128  ;IntegerCache.high == 127
         //当数值大于-128小于127时,进行缓存;否则重新new一个。
        if (i >= IntegerCache.low && i <= IntegerCache.high)
            return IntegerCache.cache[i + (-IntegerCache.low)];
        return new Integer(i);
    }
  • 4: Is c == e, i == j?

     The answers are all equal. Because when the encapsulation class is compared with the basic data type, java will automatically unbox it, and then compare whether the values ​​are equal.

In summary, several conclusions can be drawn :

1. They are all encapsulation classes, and they are all new, so they are definitely not equal. Because the memory address of the object is different.

2. They are all encapsulation classes, and they are not new. If the value is between -128 and 127, they are equal, otherwise they are not equal.

3. If the encapsulation class is compared with the basic data type, as long as the values ​​are equal, they are equal, otherwise they are not equal. Because there will be an automatic unboxing operation when the encapsulation class is compared with the basic data type.

4. They are all basic data types. If the values ​​are equal, they are equal; otherwise, they are not equal.

Transfer: the difference between Java-int and Integer_T-bright's Blog-CSDN Blog_int and integer

Guess you like

Origin blog.csdn.net/m0_60252632/article/details/124118342