Home >Java >javaTutorial >Summary of the detailed analysis of the difference between Integer and int in Java
As mentioned in the above article, why do Java generics use objects instead of primitive types? But I still don’t quite understand the difference between Integer and int, so I went on Baidu and found an article by a great guy. I thought it was pretty good, so I reposted it and shared it.
The difference between Integer and int
Integer is the encapsulation class provided by int, and int is the basic data type of Java; the default value of Integer is null, and The default value of int is 0; variables declared as Integer need to be instantiated, while variables declared as int do not need to be instantiated; Integer is an object, and a reference is used to point to this object, while int is a basic type and stores values directly.
In addition to knowing the most basic difference between Integer and int, you also need to know some other problems encountered in the actual use of Integer and int.
Otherwise, if the interviewer asks again whether Integer i = 1; int ii = 1; i==ii is true or false? It is estimated that some people will not be able to answer it. If you ask other questions, it is estimated that more people will be confused. So I summarized them and hope it will be helpful to everyone.
The code is as follows:
public class Main { public static void main(String[] args) { int a = 128; Integer b = 127; Integer c = 127; Integer d = 128; Integer e = 128; Integer f = new Integer("128"); Integer g = new Integer("127"); Integer h = new Integer("127"); //方案一() System.out.println("方案一:Integer与new Integer不同初始化方法的值的比较,Integer与new Integer不会相等"); //Integer e = 128; //Integer f = new Integer("128"); if (e == f){ System.out.println("true"); }else{ System.out.println("false"); } System.out.println("---------------------"); //方案二 System.out.println("方案二:创建两个都是直接赋值的Integer"); //Integer b = 127; //Integer c = 127; if (b == c){ System.out.println("true"); }else{ System.out.println("false"); } //Integer d = 128; //Integer e = 128; if (d == e){ System.out.println("true"); }else{ System.out.println("false"); } System.out.println("---------------------"); //方案三 System.out.println("方案三:两个都是new出来的Integer"); //Integer g = new Integer("127"); //Integer h = new Integer("127"); if (g == h){ System.out.println("true"); }else{ System.out.println("false"); } System.out.println("---------------------"); //方案四 System.out.println("方案四:int与Integer比较"); //int a = 128; //Integer f = new Integer("128"); if (a == f){ System.out.println("true"); }else{ System.out.println("false"); } } }
The running results are as follows:
方案一:Integer与new Integer不同初始化方法的值的比较,Integer与new Integer不会相等 false --------------------- 方案二:创建两个都是直接赋值的Integer true false --------------------- 方案三:两个都是new出来的Integer false --------------------- 方案四:int与Integer比较 true
Option 1:
I will first deal with the two objects e and f, Use two different initialization methods, Integer and new Integer, to compare the two objects;
Integer e = 128; Integer f = new Integer("128"); if (e == f){ System.out.println("true"); }else{ System.out.println("false"); }
Return result: false
Conclusion: Integer and new Integer will not be equal . The reference of e points to the heap, and f points to the memory (constant pool) dedicated to storing it. Their memory addresses are different, so it is false
Option 2:
Create both directly The assigned Integer is compared with the two objects;
//Integer b = 127; //Integer c = 127; if (b == c){ System.out.println("true"); }else{ System.out.println("false"); } //Integer d = 128; //Integer e = 128; if (d == e){ System.out.println("true"); }else{ System.out.println("false"); }
Return result:
true
false
Conclusion: Both are directly assigned Integer, If the number is between -128 and 127, it is true, otherwise it is false
Reason: When java compiles Integer i2 = 128, it is translated into -> Integer i2 = Integer.valueOf(128) ;The valueOf() function will cache the numbers between -128 and 127
If you look at the source code, you will understand that the numbers between -128 and 127 will be cached. When Integer b = 127 , 127 will be cached. The next time you write Integer c = 127, it will be taken directly from the cache, and it will not be new.
/** * Cache to support the object identity semantics of autoboxing for values between * -128 and 127 (inclusive) as required by JLS. * * The cache is initialized on first usage. The size of the cache * may be controlled by the {@code -XX:AutoBoxCacheMax=<size>} option. * During VM initialization, java.lang.Integer.IntegerCache.high property * may be set and saved in the private system properties in the * sun.misc.VM class. */ 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) { try { 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); } catch( NumberFormatException nfe) { // If the property cannot be parsed 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++); // range [-128, 127] must be interned (JLS7 5.1.7) assert IntegerCache.high >= 127; } private IntegerCache() {} }
Option three:
I first use the new Integer initialization method for the two objects g and h, and then compare the two objects;
Integer g = new Integer("127"); Integer h = new Integer("127"); if (g == h){ System.out.println("true"); }else{ System.out.println("false"); }
Return result: false
Conclusion: Both are new Integer objects, pointing to two different Integer objects, both are false
Option 4:
I will first check a The two objects , f, use two different initialization methods, int and new Integer, to compare the two objects;
int a = 128; Integer f = new Integer("128"); if (a == f){ System.out.println("true"); }else{ System.out.println("false"); }
Return result: true
Conclusion: The comparison between int and integer (regardless of whether new or not) is true, because Integer will be automatically unboxed into int and then compared
Related articles:
Java tutorial—the difference between int and Integer
Detailed explanation of the difference between int and Integer in Java
The above is the detailed content of Summary of the detailed analysis of the difference between Integer and int in Java. For more information, please follow other related articles on the PHP Chinese website!