一. 深入代码
 
在创建数字 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. Synplify9.6.2破解(转帖)

    Synplify9.6.2破解(转帖)   转载自:http://www.cnblogs.com/mark-sun/archive/2012/02/26/2368773.html Abstract本文 ...

  2. win7下安装curl

    先去官网下载curl,地址https://winampplugins.co.uk/curl/,我下载的版本是curl_7_52_1_openssl_nghttp2_x64.然后执行curl.exe并且 ...

  3. ios笔记一(面向对象编程)

    #import <Foundation/Foundation.h> #import "Person.h" int main (int argc, const char ...

  4. Javac编译器

    One Compiler http://www.oracle.com/technetwork/java/jvmls2016-wimmer-3125555.pdf Hacking the OpenJDK ...

  5. SQL server插入数据后,如何获取自增长字段的值?

    insert into Tb_People(uname,era,amount) values( '兆周','老年','10000') select @@identity --当运行完插入语句后,执行s ...

  6. MySQL常见的库操作,表操作,数据操作集锦及一些注意事项

    一 库操作(文件夹) 1 数据库命名规则 可以由字母.数字.下划线.@.#.$ 区分大小写 唯一性 不能使用关键字如 create select 不能单独使用数字 最长128位 2 数据库相关操作 创 ...

  7. ubuntu文件夹默认列表显示

    编辑-->首选项-->视图-->列表视图

  8. asp.net mvc 5 在没有外网win2008R2服务器部署方法

    我在本地用最新的.net 4.5和asp.net mvc 5框架做了一个小应用.本地都测试打包成功. 现在要放到服务器上,这个应用只是内网用.服务器不允许连接外网.看到www.asp.net 没有mv ...

  9. shadowshocks下载地址

    https://github.com/shadowsocks/shadowsocks-windows/releases

  10. Java代码编写的30条建议

    1) 类名首字母应该大写.字段.方法以及对象(句柄)的首字母应小写.对于所有标识符,其中包含的所有单词都应紧靠在一起,而且大写中间单词的首字母.例如: ThisIsAClassName thisIsM ...