This is the third day of my participation in the More text Challenge. For details, see more text Challenge
introduce
Autoboxing and unboxing for basic data types are features that have been available since J2SE 5.0. That’s what the compiler does for us.
- Autoboxing, which wraps basic types with their corresponding reference types
For example, convert an int to an Integer
- Automatic unpacking, the packaging type into the basic data type;
For example, converting an Integer to an int
example
I’m going to start with a piece of code, so you can analyze what it looks like when you run it, and then see if it looks the same.
public static void main(String[] args) {
Integer i1 = new Integer(1);
Integer i2 = new Integer(1);
System.out.println(i1 == i2);
//-------------------------------------------
Integer i3 = 127;
Integer i4 = 127;
int i5 = 127;
System.out.println(i3 == i4);
System.out.println(i3 == i5);
//-------------------------------------------
Integer i6 = 128;
Integer i7 = 128;
System.out.println(i6 == i7);
}
Copy the code
parsing
Integer i1 = new Integer(1);
Integer i2 = new Integer(1);
System.out.println(i1 == i2); //false
Copy the code
Because new compares the addresses of two objects, the result is false;
Integer i3 = 127;
Integer i4 = 127;
int i5 = 127;
System.out.println(i3 == i4); //true
System.out.println(i3 == i5); //true
Integer i6 = 128;
Integer i7 = 128;
System.out.println(i6 == i7); //false
Copy the code
- i3 == i4;
For Integer i3 = 127; ValueOf (int I) for values between — 128 and 127 (the default is 127), ValueOf (int I) returns a cached Integer object (not a new one), so i3 == i5 returns true
- i6 == i7
public static Integer valueOf(int i) {
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}
Copy the code
So 128 is out of the range of the cache so we need a new object, so i6 == i7 is false for the two objects that are comparing the address values
- i3 == i5
The system calls intValue() to convert the Integer type to an int, so i3 is compared with i5, and the result is true
Integer class related part of the source code
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(a) {}}public static Integer valueOf(int i) {
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}
Copy the code