高效的Integer.valueOf小结

  在PMD中,都建议使用的Integer.valueOf,
avoid instantiating integer objects. Call Integer.valueOf() instead.

历史:JDK1.5后增加了Integer.valueOf. 因此1.5前不能用.
优点:查看1.5源码,看看到Integer.ValueOf(int)里面做了什么优化


public static Integer valueOf(int i) { 
  final int offset = 128; 
  if (i >= -128 && i <= 127) { // must cache 
    return IntegerCache.cache[i + offset]; 
  } 
  return new Integer(i); 
}  
 
private static class IntegerCache { 
  private IntegerCache(){} 
     
  static final Integer cache[] = new Integer[-(-128) + 127 + 1]; 
  static { 
  for(int i = 0; i < cache.length; i++) 
     cache = new Integer(i - 128); 
  } 
}  
从源代码可以知道,ValueOf对-128~127这256个值做了缓存(IntegerCache),如果int值的范围是:-128~127,在ValueOf(int)时,他会直接返回IntegerCache的缓存给你。


public static void main(String []args) { 
         Integer a = 10; 
         Integer b = 10; 
         System.out.println(a==b); 
         Integer c = new Integer(10); 
         Integer d = new Integer(10); 
         System.out.println(c==d);
        
         Integer e = 200;
         Integer f = 200;
         System.out.println(e == f);
    }

结果是:
true
false
false
因为:java在编译的时候 Integer a = 10; 被翻译成-> Integer a = Integer.valueOf(10);,所以a和b得到都是一个Cache对象,并且是同一个!而c和d是新创建的两个不同的对象,所以c自然不等于d。然而,当数据超出-128~127范围时,就不会先做Integer e = Integer.valueOf(200)这样的操作了。


   1、new Integer():方法会在内存中新开辟一个内存空间;

2、valueOf():方法只有在传入的参数<-128或>127时,才会去调用一个new Integer()方法去创建一个新的对象,否则会使用静态类IntegerCache中的cache里的对象。

程序表象:

如果我们要调用50万次的new Integer(4),在不去设置jvm内存参数的情况下会outOfMemery,而Integer.valueOf(4)则不会。

我们可以分别用new Integer(4)和valueOf(4)构件5万个对象,用jmap -histo:live查看一下内存中Integer对象实例的个数:

new Integer(4)情况下:



num     #instances         #bytes  class name

----------------------------------------------

   1:        500259        8004144  java.lang.Integer

valueOf(4)情况下:



26:           259           4144  java.lang.Integer



对于,第二点:“valueOf():方法只有在传入的参数<-128或>127时,才会去调用一个new Integer()方法去创建一个新的对象,否则会使用静态类IntegerCache中的cache里的对象。”

这里只是jdk的默认值,其实可以通过启动参数-XX:AutoBoxCacheMax修改掉最大范围,但最大值也不会超过:Integer.MAX_VALUE +128

你可能感兴趣的:(Integer)