Difference and comparison between int and Integer in java (==)

foreword

  int is the base type and Integer is its wrapper class.

  The difference between int and Integer is the difference between the basic data type and its wrapper class in a big way;

The difference between int and Integer

     1. When declaring variables

int i; //The default initial value is 0
Integer j; //The default initial value is null

   Data types in Java are divided into basic data types and reference data types, int is a basic type, and Integer is a reference data type (wrapper class); therefore, when initializing, the default initial value of the int variable is 0; and the default value of the Integer variable is 0 . The initial value is null.

     2. When initializing

int i =1;
Integer i= new Integer(1);

   Note: Due to the automatic packing and unpacking ( starting with jdk1.5 ), the initialization of the Integer class can also be used: Integer i= 1;    

   The relationship between int and Integer

 1.int is the basic data type;

 2. Integer is the encapsulation class of int;

 3. Both int and Integer can represent a certain value;

 4. Int and Integer cannot be used interchangeably because they are two different data types;

     int is a basic data type (traces left by the process, but it is a useful supplement to java), Integer is a class, an extension (wrapper) of int, defines a lot of conversion methods, and also provides when dealing with int types. A few other constants and methods that are very useful.  Similar also: float - Float; double - Double; boolean - Boolean and so on.

     For example: when you need to put things in ArrayList and HashMap, built-in types such as int and double cannot be put in, because the containers are all loaded with objects, which requires the outer classes of these built-in types. .

     Every built-in type in Java has a corresponding outer class.

  for example

 ArrayList al=new ArrayList();
 int n=40;
 Integer nI = new Integer (n);
 al.add(n);//No
 al.add(nI);//Yes

  And the generic definition does not support int: For example: List<Integer> list = new ArrayList<Integer>(); Yes but List<int> list = new ArrayList<int>(); not.

All in all: if we define a number of type int, it is only used to perform some addition, subtraction, multiplication and division operations or pass it as a parameter, then we can directly declare it as an int basic data type, but if we want to handle it like an object, then Use Integer to declare an object, because java is an object-oriented language, so when declared as an object, it can provide many ways to convert between objects and some commonly used methods. Since Java is an object-oriented language, it is better to declare a variable in object format, which is more conducive to your understanding of object-oriented.

Similarities and differences between primitive types and wrapper types

     1. In Java, everything is an object, but the eight basic types (char, byte, int, double, float, short, long, boolean) are not objects.

     2. The declaration method is different. The basic type does not need to be created by the new keyword, while the encapsulated type needs the new keyword.

     3. The storage method and location are different. The basic type directly stores the value of the variable and saves it in the stack for efficient access. The encapsulated type needs to point to the instance by reference, and the specific instance is stored in the heap.

     4. The initial value is different. The initial value of the encapsulated type is null, and the initial value of the basic type depends on the specific type. For example, the initial value of the int type is 0 (integer: including int, short, byte, long, the initial value is 0), the boolean type is false, the float type: float, double, the initial value is 0.0, the character: char, the initial value is a space, that is, '' ", if it is output, the effect will not be seen on the Console.

     5. The usage is different, for example, only the packaging type can be used in cooperation with the collection class.

   Comparison of int and Integer

      If the interviewer asks the difference between Integer and int: It is estimated that most people will only say two points: Ingeter is a wrapper class of int, pay attention to a class; the initial value of int is 0, and the initial value of Ingeter is null. But what if the interviewer asks again if Integer i = 1; int ii = 1; i==ii is true or false? It is estimated that some people can't answer, and if you ask others, it is estimated that more people will be confused. So I summarize them in the hope that it will be helpful to everyone.

First look at the code:

public class Test {

    /**
     * @param args
     */
    public static void main(String[] args) {
        int i = 128;
        Integer i2 = 128;
        Integer i3 = new Integer(128);
        //Integer will be automatically unboxed to int, so it is true
        System.out.println(i == i2);
        System.out.println(i == i3);
        System.out.println("**************");
        Integer i4 = 127; //java在编译的时候,被翻译成-> Integer i4 = Integer.valueOf(127);
        Integer i5 = 127;
        System.out.println(i4 == i5);//true
        Integer i6 = 128;
        Integer i7 = 128;
        System.out.println(i6 == i7);//false
        Integer i8 = new Integer(127);
        System.out.println(i5 == i8); //false
        Integer i9 = new Integer(128);
        Integer i10 = new Integer(123);
        System.out.println(i9 == i10);  //false
    }

}

首先,11行和12行输出结果都为true,因为Integer和int比较都会进行自动拆箱(jdk1.5以上)。

16行的结果为true,而19行则为false,很多人都不懂为什么。其实java在编译Integer i4 = 127的时候,被翻译成-> Integer i4 = Integer.valueOf(127);所以关键就是看valueOf()函数了。只要看看valueOf()函数的源码就会明白了。JDK源码的valueOf函数式这样的:

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

看一下源码大家都会明白,对于-128到127之间的数,会进行缓存,Integer i4 = 127时,会将127进行缓存,下次再写Integer i5 = 127时,就会直接从缓存中取,就不会new了。所以16行的结果为true,而19行为false。

对于21行和24行,因为对象不一样,所以为false。

我对于以上的情况总结如下:

  ①无论如何,Integer与new Integer不会相等。不会经历拆箱过程,i8的引用指向堆,而i5指向专门存放他的内存(常量池),他们的内存地址不一样,所以为false
  ②两个都是非new出来的Integer,如果数在-128到127之间,则是true,否则为false
  java在编译Integer i2 = 128的时候,被翻译成-> Integer i2 = Integer.valueOf(128);而valueOf()函数会对-128到127之间的数进行缓存,而128>127,所以实际上Integer i2 = new  Integer(128);
  ③两个都是new出来的,都为false
  ④int和integer(无论new否)比,都为true,因为会把Integer自动拆箱为int再去比较




Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324822593&siteId=291194637