一. 深入代码
 
在创建数字 1 的对象时, 大多数人会使用 new Integer(1), 而使用 Integer.valueOf(1) 可以使用系统缓存,既减少可能的内存占用,也省去了频繁创建对象的开销。 

系统默认只缓存 -128~127 之间的整数。下面我们看一下 Integer.valueOf(int) 方法的代码:

 public static Integer valueOf(int i) {
assert IntegerCache.high >= ;
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}
注意到核心 IntegerCache.cache[i + (-IntegerCache.low)], 观察 IntegerCache 类的源码实现:
为 -128 ~ 127 数值提供自动装箱的缓存服务。  
 private static class IntegerCache {
static final int low = -;
static final int high;
static final Integer cache[]; static {
// high value may be configured by property
int h = ;
String integerCacheHighPropValue =
sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
if (integerCacheHighPropValue != null) {
int i = parseInt(integerCacheHighPropValue);
i = Math.max(i, );
// Maximum array size is Integer.MAX_VALUE
h = Math.min(i, Integer.MAX_VALUE - (-low) -);
}
high = h; cache = new Integer[(high - low) + ];
int j = low;
for(int k = ; k < cache.length; k++)
cache[k] = new Integer(j++);
} private IntegerCache() {}
}
static 静态代码块可知缓存的初始化是在第一次使用的时候。 通过 VM 参数-XX:AutoBoxCacheMax=<size> 可以配置缓存的最大值。 在 VM 初始化期间, 缓存最大值 high, 可能被保存在 sun.misc.VM class 的私有系统属性里。      
 
 
三. 总结
 
除非是 JDK 1.5 以前的环境, 如果系统不需要新对象, 则推荐使用 Long, Integer, Short, Character, Byte 的 valueOf() 方法提升性能。 

Integer.valueOf的更多相关文章

  1. Integer.valueof(String s)和Integer.parseInt(String s)的具体区别是什么?

    Integer.valueof(String s)和Integer.parseInt(String s)的具体区别是什么? Integer.valueof(String s)是将一个包装类是将一个实际 ...

  2. Integer.parseInt(String s) 和 Integer.valueOf(String s) 的区别

    通过查看java.lang.Integer的源码可以发现, 它们最终调用的都是 /** * Parses the string argument as a signed integer in the ...

  3. Integer.valueOf(String) 方法之惑

    本文由 ImportNew - 靳禹 翻译自 stackoverflow.欢迎加入翻译小组.转载请见文末要求. 有个仁兄在 StackOverflow 上发起了一个问题,是这么问的: “ 我被下面的代 ...

  4. [转]Integer.valueOf(String) 方法之惑

    具体问题以前偶然遇到过,好象是一个java答题得分的论坛,当时做错还研究了下怎么回事,但是前两天遇到类似问题却没想起来.巩固下基础,转了下面文章. 以下内容转自:http://www.importne ...

  5. Integer.parseInt()和Integer.valueOf()有什么区别

    jdk的源代码的时候注意到Integer.parseInt(s) 和 Integer.valueOf(s)的具体代码的实现有所区别: Java代码 public static int parseInt ...

  6. Integer.valueOf与Integer.parseInt的小疑惑

    参考博客: http://www.importnew.com/9162.html 测试代码如下: public class Main { /** * 备注:结果跟你的JDK版本有关系: * * 我的是 ...

  7. new Integer(1)和Integer.valueOf(1)的区别

    java.lang包中的Integer类是我们比较常用的类,比如以下代码: Integer a=new Integer(1) Integer a=Integer.valueOf(1); 两个都是得到一 ...

  8. Integer.valueOf(int)及自动装箱内幕

    Integer为什么要提供功能与new Integer(xx)一样的valueOf(xx)方法呢,看了源代码之后,我发现了惊人的内幕. public static Integer valueOf(in ...

  9. JAVA中Integer.valueOf, parsetInt() String.valueOf的区别和结果

    先来看段代码 public class IntegerDemo { public static void main(String[] args) { String num = null; System ...

  10. Integer.valueOf()与Integer.parseInt()区别

    Integer.parseInt()和Integer.valueOf()有本质区别,具体如下列: Integer.parseInt()把String   型转换为Int型,  Integer.valu ...

随机推荐

  1. ASP.NET Web API 中 特性路由(Attribute Routing) 的重名问题

    刚才忘了说了,在控制器名重名的情况下,特性路由是不生效的.不然的话就可以利用特性路由解决同名的问题了. 而且这种不生效是真的不生效,不会提示任何错误,重名或者什么的,直接会报告404,所以也是个坑.

  2. Java中关于日期类那些方法

                                                                       转载请注明出处http://blog.csdn.net/harry ...

  3. Python学习笔记11:标准库之文件管理(os包,shutil包)

    1 os包 os包包含各种各样的函数,以实现操作系统的很多功能.这个包很庞杂.os包的一些命令就是用于文件管理. 我们这里列出最经常使用的: mkdir(path) 创建新文件夹.path为一个字符串 ...

  4. [Codility] CommonPrimeDivisors

    A prime is a positive integer X that has exactly two distinct divisors: 1 and X. The first few prime ...

  5. Spring-两种配置容器

    Spring提供了两种容器类型       SpringIOC容器是一个IOC Service Provider.提供了两种容器类型:BeanFactory和ApplicationContext.Sp ...

  6. 每日英语:China Poses Challenge for Coal

    China's appetite for coal, once seemingly unlimited, is starting to wane, and the effects are rippli ...

  7. Laravel4.2取得配置文件值

    Config::get('app.timezone'); laravel 使用Config::get方法来取得配置文件的值 laravel的配置文件的位置们于app.config文件夹的php文件中, ...

  8. C#中调用PowerShell代码

    在C#中调用PowerShell代码,很多时候Add是不好使的!要用AddScript!记录一下! using (Runspace runspace = RunspaceFactory.CreateR ...

  9. deepin linux手工更新系统

    sudo apt-get updatesudo apt-get dist-upgrade -y 可以使用阿里云的镜像

  10. Android实例-ImageList与Image的应用

    procedure TForm1.Button1Click(Sender: TObject);var  oSizeF: TSizeF;beginoSizeF.cx:=10;//发现这个值小了会增加马赛 ...