Question: what is the difference between int and Integer, and the value cache range of Integer?
1, Basic comparison of int and Integer
Int is the basic data type. It stores values directly. During initialization, the initial value of the variable of int class is 0;
Integer is an object, which is pointed to by a reference. The variable initialization value of integer is null.
The following code:
ArrayList a = new ArrayList(); int n = 4; Integer n1 = new Integer(n); a.add(n);//An error is reported and the compilation fails. a.add(n1);// After compilation, you can add
①. Classification of two data types in Java
Original data types: boolean, byte, short, char, int, float, double, long;
Reference data types are divided into arrays and interfaces.
②. Java provides encapsulated classes for each primitive type
For the convenience of programming and development, Java still introduces basic data types. However, in order to operate these basic data types as objects, Java introduces the corresponding wrapper class for each basic data type. The wrapper class of int is Integer. Since Java5, automatic boxing and automatic unpacking have been introduced, so that they can be converted to each other.
Original data types: boolean, char, byte, short, int, long, float, double
Encapsulation class types: Boolean, Character, Byte, Short, Integer, Long, Float, Double
2, Basic analysis
① Auto boxing: converts basic data classes back to objects
public class Test{ public static void main(String[] args){ // Declare an Integer object Integer num = 10; // The above statement uses automatic packing: //Resolves to integer num = integer valueOf(10); //The valueof (parameter) method actually calls new Integer(10); } }
10 belongs to the basic data type. In principle, it cannot be directly assigned to an object Integer, but such a declaration can be made after JDK5. The data type can be automatically converted into the corresponding encapsulated class. After becoming an object, all the methods declared by the object can be called.
② Auto unpacking: converts objects back to basic data types
public class Test{ public static void main(String[] args ){ // Declare an Integer object Integer a = 10; // The following calculation implies automatic unpacking System.out.println( a--); } }
Because objects cannot be calculated directly, they must be converted to basic data types to add, subtract, multiply and divide.
3, In depth analysis
① . description
public class Test { public static void main(String[] args) { //Number outside - 128 ~ 127 Integer num1 = 128; Integer num2 = 128; System.out.println(num1==num2); //false // Number within - 128 ~ 127 Integer num3 = 9; Integer num4 = 9; System.out.println(num3==num4); //true } }
Analysis reason: due to Java's design of automatic packing and unpacking of Integer and int, it is a pattern called flyweight.
Increase the reuse of simple numbers. Java defines that during automatic boxing, for values between - 128 and 127, after they are boxed as Integer objects, they will be reused in memory, and there will always be only one object.
② Integer source code analysis
When an Integer object is assigned an int value, the static method valueOf Integer class will be called. The source code is as follows:
public static Integer valueOf(String s , int radix) throws NumberFormatException{ return Integer.valueOf(parseInt(s,radix)); } // Fetch from cache between - 128 and 127 public static Integer valueOf(int i){ if( i >= IntegerCache.low && i <= IntegerCatche.high){ return IntegerCache.cache[i +(-IntegerCache.low)]; } return new Integer(i); }
IntegerCache is an internal class of Integer. The source code is as follows:
private static class IntegerCache{ static final int low = -128; static final int high; static final Integer cache[]; static { // high value may be configure by property int h = 127; String integerCacheHighPropValue = sun.misc.VM.getSaveProperty("java.lang.IntegerCache.high"); if(integerCacheHighPropValue != null) { try{ int i=parseIn(integerCacheHighPropValue); i = Math.max(i,127); h = Math.min(i,Integer.MAX_VALUE -(-low)-1); }catch(NumberFormat nfe){ // if the propvalue cannot be pared into an int,ignore it } } high = h; cache = new Integer[(high - low)+1]; int j = low; for(int k = 0 ; k< cache.length ; k ++) cache[k] = new Integer(j++); assert IntegerCache.high >= 127; } private IntegerCache(){} }