What is the difference between int and Integer based on Java

http://blog.csdn.net/chenliguan/article/details/53888018

Original December 26, 2016 17:47:03
0 Basic usage comparison of int and integer
Int is a basic type, which directly stores the value. When initializing, the variable of the int class is initially 0.
Integer is an object, and a reference is used to point to this object, and the variable of Integer is initialized to null.
ArrayList al=new ArrayList();
  int n=40;
  ​​Integer nI=new Integer(n);
  al.add(n);//No
  al.add(nI);//Yes

1 Classification of two data types in Java
Primitive data types, divided into boolean, byte, int, char, long, short, double, float
reference data types, divided into arrays, classes, interfaces
2 Java provides encapsulation classes for each primitive type
For the convenience of programming, basic data types are introduced, but in order to be able to operate these basic data types as objects, Java introduces a corresponding wrapper class for each basic data type. The wrapper class of int is Integer, from Java 5 began to introduce the autoboxing/unboxing mechanism, making the two interchangeable.
Primitive types: boolean, char, byte, short, int, long, float, double
Encapsulated class types: Boolean, Character, Byte, Short, Integer, Long, Float, Double

2 Basic parsing
2.1 Automatic boxing: reconverting basic data types into objects
public class Test {
public static void main(String[] args) {
//declare an Integer object
Integer num = 9;

        //以上的声明就是用到了自动的装箱:解析为:Integer num = new Integer(9);
    }  
}  

9 is a basic data type. In principle, it cannot be directly assigned to an object Integer, but you can make such a declaration after jdk1.5. The basic data type is automatically converted into the corresponding encapsulation type, and all the methods declared by the object can be called after becoming an object.
2.2 Automatic unboxing: reconvert the object to the basic data type
public class Test {
public static void main(String[] args) {
//declare an Integer object
Integer num = 9;

        //进行计算时隐含的有自动拆箱
        System.out.print(num--);
    }  
}  

Because the object cannot be directly operated, but must be converted into a basic data type before addition, subtraction, multiplication and division can be performed.
Contrast:
/boxing
Integer num = 10;
//unboxing
int num1 = num;

3 In-depth analysis
3.1 situation description
public class Test {
public static void main(String[] args) {
//Numbers outside -128~127
Integer num1 = 128; Integer num2 = 128;
System.out.println(num1= =num2); //false

        // 在-128~127 之内的数 
        Integer num3 = 9;   Integer num4 = 9;   
        System.out.println(num3==num4);   //true
    }  
}  

Reason for analysis: It is due to java's design of automatic boxing and unboxing of Integer and int, which is a pattern: called flyweight.
To increase the reuse of simple numbers, Java defines that for values ​​ranging from –128 to 127 during autoboxing, after they are boxed as Integer objects, they will be reused in memory, and there will always be only one object.
If it exceeds the value from –128 to 127, the boxed Integer object will not be reused, which is equivalent to creating a new Integer object every time it is boxed.
3.2 Integer source code
analysis When assigning an int value to an Integer object, the static method valueOf of the 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));
}

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

IntegerCache is the inner class of Integer, the source code is as follows:
/**
* The cache supports the object identification semantics of auto-boxing
* -128 and 127 (inclusive).
*
* The cache is initialized on first use. The size of the cache
* can be controlled by the -XX:AutoBoxCacheMax= option.
* During VM initialization, the java.lang.Integer.IntegerCache.high property
* can be set and saved in a private system property
*/
private static class IntegerCache {
static final int low = -128;
static final int high;
static final Integer cache [];

    static {
        // high value may be configured by property
        int h = 127;
        String integerCacheHighPropValue =
            sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
        if (integerCacheHighPropValue != null) {
            int i = parseInt(integerCacheHighPropValue);
            i = Math.max(i, 127);
            // Maximum array size is Integer.MAX_VALUE
            h = Math.min(i, Integer.MAX_VALUE - (-low) -1);
        }
        high = h;

        cache = new Integer[(high - low) + 1];
        int j = low;
        for(int k = 0; k < cache.length; k++)
            cache[k] = new Integer(j++);
    }

    private IntegerCache() {}
}

4 Reference link
java autoboxing and unboxing http://www.cnblogs.com/shenliang123/archive/2012/04/16/2451996.html
What is the difference between int and Integer?
http://www.cnblogs.com/ shenliang123/archive/2012/04/16/2451996.html

Guess you like

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