First look at the implementation of valueof ()
public static Integer ValueOf (String s) throws NumberFormatException {
return integer.valueof (parseint (s));
}
public static Integer valueOf (int i) {
assert integercache.high >= 127;
if (i >= integercache.low && i <= integercache.high)
return integercache.cache[i + (-integer Cache.low)];
return new Integer (i);
}
So it can be seen as:
New Integer (Integer.parseint (s))
In other words, the result of parseint is encapsulated once and the return value is integer
You can also take a look at integer and int unpacking
int i = 128; Integer i2 = 128;
Integer i2 = integer.valueof (128);
Integer i3 = new Integer (128); System.out.println ("i = = I2:" + (i = = I2)),//integer automatically unboxing to int, so true System.out.println ("i = = i3:" + (i = = i3));//i Nteger automatically unboxing to int, so true System.out.println ("i2 = = i3:" + (I2 = = i3));//new integer = = new Integer (128), so Fals E//For the number between 128 to 127, will be cached, integer i5 = 127, 127 will be cached, the next time you write an Integer I6 = 127, will be directly from the cache, will not be new Inte
GER i5 = 127;//java at compile time, translated into Integer i5 = integer.valueof (127);
Integer I6 = 127;
SYSTEM.OUT.PRINTLN ("i5 = = I6:" + (i5 = = I6));
Integer i52 = 128;
Integer i62 = 128;
System.out.println ("i52 = = i62:" + (i52 = = i62));
Integer ii5 = new Integer (127); SYSTEM.OUT.PRINTLN ("i5 = = Ii5:" + (i5 = = ii5));
Because the objects are different, false integer i7 = new Integer (1);
Integer i8 = new Integer (1); System.out.println ("i7 = = I8: "+ (i7 = = i8)); False because the object is not the same