java关于Integer设置-128到127的静态缓存
今天在一个java群里,看到有个群友问到如下为什么第一个为true,第二个为false。
System.out.println(Integer.valueOf("50")==Integer.valueOf("50")); //true
System.out.println(Integer.valueOf("200")==Integer.valueOf("200")); //false
由于一开始他问的第二句,我还想当然的以为是new的对象,肯定不一样,但是为什么第一句为true呢,后来通过查找资料发现
1、https://www.zhihu.com/question/29879295/answer/102396251
2、http://blog.csdn.net/chengzhezhijian/article/details/9628251

/**
* Returns a <tt>Integer</tt> instance representing the specified
* <tt>int</tt> value.
* If a new <tt>Integer</tt> instance is not required, this method
* should generally be used in preference to the constructor
* {@link #Integer(int)}, as this method is likely to yield
* significantly better space and time performance by caching
* frequently requested values.
*
* @param i an <code>int</code> value.
* @return a <tt>Integer</tt> instance representing <tt>i</tt>.
* @since 1.5
*/
public static Integer valueOf(int i) {
if(i >= -128 && i <= IntegerCache.high)
return IntegerCache.cache[i + 128];
else
return new Integer(i);
}


private static class IntegerCache {
static final int high;
static final Integer cache[];
static {
final int low = -128;
// high value may be configured by property
int h = 127;
if (integerCacheHighPropValue != null) {
// Use Long.decode here to avoid invoking methods that
// require Integer's autoboxing cache to be initialized
int i = Long.decode(integerCacheHighPropValue).intValue();
i = Math.max(i, 127);
// Maximum array size is Integer.MAX_VALUE
h = Math.min(i, Integer.MAX_VALUE - -low);
}
high = h;
cache = new Integer[(high - low) + 1];
int j = low;
for(int k = 0; k < cache.length; k++)
cache[k] = new Integer(j++);
}
private IntegerCache() {}
}

valueOf会将常用的值(-128 to 127)cache起来。当i值在这个范围时,会比用构造方法Integer(int)效率和空间上更好。
java关于Integer设置-128到127的静态缓存的更多相关文章
- java中Integer与int装箱拆箱一点收获
示例代码: class BoxIntInteger { public static void main(String[] args) { Integer a = new Integer(10111); ...
- Java中Integer类型的整数值的大小比较
如果比较两个数值相等的Integer类型的整数,我们可能会发现,用"=="比较(首先你必须明确"=="比较的是地址),有的时候返回true,而有的时候,返回fa ...
- Java: Integer用==比较时127相等128不相等的原因
直接看问题吧 for (int i = 0; i < 150; i++) { Integer a = i; Integer b = i; System.out.println(i + " ...
- Integer a= 127 与 Integer b = 128相关
Integer a = 127; Integer b = 127; Integer c = 128; Integer d = 128; a == b 与 c == d 的比较结果是什么? a == b ...
- java中为什么byte的取值范围是-128到+127
概念:java中用补码表示二进制数,补码的最高位是符号位,最高位为“0”表示正数,最高位为“1”表示负数.正数补码为其本身:负数补码为其绝对值各位取反加1:例如:+21,其二进制表示形式是000101 ...
- Integer自动装箱拆箱bug,创建对象在-128到127
1 public class Demo3 { public static void main(String[] args) { Integer a = 1; Integer b = 2; Intege ...
- java中byte取值范围为什么是 -128到127
概念:java中用补码表示二进制数,补码的最高位是符号位,最高位为“0”表示正数,最高位为“1”表示负数.正数补码为其本身:负数补码为其绝对值各位取反加1:例如:+21,其二进制表示形式是000101 ...
- java.lang.Integer源码浅析
Integer定义,final不可修改的类 public final class Integer extends Number implements Comparable<Integer> ...
- 再学Java 之 Integer 包装类缓存
前言:本博文将涉及的Java的自动装箱和自动拆箱,可以参考 这篇文章 和 官方教程 ,这里不再赘述. 首先,先看一个小程序: public class Main { public static voi ...
随机推荐
- python——操作系统的发展史
一.手工操作 —— 穿孔卡片 1946年第一台计算机诞生--20世纪50年代中期,计算机工作还在采用手工操作方式.此时还没有操作系统的概念. 程序员将对应于程序和数据的已穿孔的纸带(或卡片)装入 ...
- Bootstrap源码
1.Bootstrap Bootstrap是美国Twitter公司的设计师Mark Otto和Jacob Thornton合作基于HTML.CSS.JavaScript 开发的简洁.直观.强悍的前端开 ...
- javascript/js实现 排序二叉树数据结构 学习随笔
二叉树是一种数据结构.其特点是: 1.由一系列节点组成,具有层级结构.每个节点的特性包含有节点值.关系指针.节点之间存在对应关系. 2.树中存在一个没有父节点的节点,叫做根节点.树的末尾存在一系列没有 ...
- PHP原生EXCEL导出带样式无插件无乱码实现
PHP原生EXCEL导出 经测试 带样式 无插件 无乱码,不需要引入任何插件,不需要修改任何编码 (使用时只需要修改引入php数据库配置文件.修改thead tbody中的数据即可.根据自己的需要去接 ...
- MACOS 安装mysqlclient 的 Library not loaded错误
报错场景 >>> import MySQLdb Traceback (most recent call last): File "<stdin>", ...
- 读取普通java web项目下的WEB-INF目录下的配置文件(application.xml,xx.properties等配置文件)
一.在Java web工程WEB-INF下创建weixin.properties属性文件 weixin.properties属性文件里内容如下: 二.创建PropertiesUtils.java测试类 ...
- awk 常用选项及数组的用法和模拟生产环境数据统计
awk 常用选项总结 在 awk 中使用外部的环境变量 (-v) awk -v num2="$num1" -v var1="$var" 'BEGIN{print ...
- Nginx的反向代理和负载均衡服务
Nginx (engine x) 是一个高性能的HTTP和反向代理服务,也是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个BSD-like 协议下发行. ...
- XSS挑战之旅平台通关练习
1.第一关 比较简单,测试语句: <svg/onload=alert(1)> <script>confirm(1)</script> <script>p ...
- AD19新功能之交互式等长
多信号线等长 选中需要等长的信号线: 选择“Interaction Length Tuning”命令,然后在网络线上点击一下,然后 tab 键暂停: 在Properties面板中,修改Source部分 ...