Explain the difference between int and Integer in Java in detail

  • 2021-07-13 05:25:21
  • OfStack

Basic data types and reference types

Java is an object-oriented programming language, 1 cut is an object, but for the convenience of programming, basic data types are introduced. In order to operate these basic data types as objects, Java introduces corresponding packaging types (wrapper class) for every basic data type, and the packaging class of int is Integer. From Java 5, automatic packing/unpacking mechanism is introduced, so that the two can convert each other, corresponding to the following:

Original types: boolean, char, byte, short, int, long, float, double
Packaging type: Boolean, Character, Byte, Short, Integer, Long, Float, Double
There are only the above eight basic data types in Java, except for the basic type (primitive type), and the rest are reference types (reference type).

3 Reference types:

Class class Interface interface Array array

Difference between int and Integer

1. Integer is the wrapper class of int, and int is a basic data type of java 2. The Integer variable must be instantiated before it can be used, while the int variable does not 3. Integer is actually a reference to an object. When new1 has Integer, it actually generates a pointer to this object; int stores data values directly 4. The default value of Integer is null, and the default value of int is 0

Extension:
Comparison between Integer and int

1. Because the Integer variable is actually a reference to an Integer object, two Integer variables generated by new are always not equal (because new generates two objects with different memory addresses).


Integer i = new Integer(100);
Integer j = new Integer(100);
System.out.print(i == j); //false

2. When the Integer variable is compared with the int variable, the result is true as long as the values of the two variables are equal (because when the wrapper class Integer is compared with the basic data type int, java will be automatically unwrapped into int, and then compared, which actually becomes a comparison between two int variables)


Integer i = new Integer(100);
int j = 100 ; 
System.out.print(i == j); //true

3. When the Integer variable generated by non-new is compared with the variable generated by new Integer (), the result is false. (Because the Integer variable generated by non-new points to an object in the java constant pool, and the variable generated by new Integer () points to a newly created object in the heap, they have different addresses in memory.)


Integer i = new Integer(100);
Integer j = 100;
System.out.print(i == j); //false

4. When comparing two Integer objects that are not generated by new, if the values of two variables are between-128 and 127, the comparison result is true, and if the values of two variables are not in this interval, the comparison result is true


falseInteger i = 100;
Integer j = 100;
System.out.print(i == j); //true

Not between-128-127:


Integer i = 128;
Integer j = 128;
System.out.print(i == j); //false

For the reasons of Article 4:
java compiles Integer i = 100; Will be translated into
Integer i = Integer. valueOf (100); And the definition of valueOf of Integer type in java API is as follows:


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

java caches numbers between-128 and 127. When Integer i = 127, 127 will be cached. When Integer j = 127 is written again, it will be taken directly from the cache, so new will not be used.


Related articles: