终极蛇皮上帝视角之铁头娃之鲁迅之暑假闲的慌之bilibili看尚学堂网课的非洲酋长java小复习...

转自https://www.sxt.cn/Java_jQuery_in_action/eight-cache-problem.html

第一个点

终极蛇皮上帝视角之铁头娃之鲁迅之暑假闲的慌之bilibili看尚学堂网课的非洲酋长java小复习..._第1张图片

自动装箱与拆箱的功能是所谓的“编译器蜜糖(Compiler Sugar)”

自动装箱调用的是valueOf()方法,而不是new Integer()方法。

自动拆箱调用的xxxValue()方法。

 

第二个点

【示例8-8】IntegerCache类相关源码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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() {}
}

      由上面的源码我们可以看到,静态代码块的目的就是初始化数组cache的,这个过程会在类加载时完成。

终极蛇皮上帝视角之铁头娃之鲁迅之暑假闲的慌之bilibili看尚学堂网课的非洲酋长java小复习..._第2张图片

 

 

因为那个一开始就把-128~127的东西加到cache数组,所以一样

1234不在cache范围,所以不一样

 终极蛇皮上帝视角之铁头娃之鲁迅之暑假闲的慌之bilibili看尚学堂网课的非洲酋长java小复习..._第3张图片

 

转载于:https://www.cnblogs.com/xx123/p/11146842.html

你可能感兴趣的:(java)