Integer缓存范围-128 to 127
1. 源码
/*** Returns an {@code Integer} instance representing the specified* {@code int} value. If a new {@code Integer} 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.** This method will always cache values in the range -128 to 127,* inclusive, and may cache other values outside of this range.** @param i an {@code int} value.* @return an {@code Integer} instance representing {@code i}.* @since 1.5*/public static Integer valueOf(int i) {if (i >= IntegerCache.low && i <= IntegerCache.high)return IntegerCache.cache[i + (-IntegerCache.low)];return new Integer(i);}
2. 测试
public static void main(String[] args) {Integer a0 = 128; // 相当于Integer.valueOf(128)Integer a = Integer.valueOf(128);Integer b = Integer.valueOf(128);System.out.println(a == b); // falseSystem.out.println(a0 == b); // falseSystem.out.println(128 == new Integer(128)); // Integer自动拆箱,trueInteger c = Integer.valueOf(127);Integer d = Integer.valueOf(127);System.out.println(c == d); // true
}
3. 总结
- int类型比较,相等;
- 两个Integer比较, 在[-128, 127]内相等,否则不相等,因为会重新new一个对象;
- 清楚自动装箱(Integer a0 = 128)和自动拆箱(int a = Integer.valueOf(128));
Integer缓存范围-128 to 127相关推荐
- Integer缓存问题分析
Integer缓存问题分析 <阿里巴巴代码规约>中OOP规约部分,第七条:[强制]所有整型包装类对象之间值的比较,全部使用 equals 方法比较. 说明:对于 Integer var = ...
- 【java】java Integer 缓存 一定是 -128~127 吗
1.概述 我们都知道 java Integer是有缓存的,缓存范围是 -128~127.但是这个值是一定的吗? 不一定.看源码.缓存大小居然可以改变 /*** Cache to support the ...
- Integer的自动拆装箱的陷阱(整型数-128到127的值比较问题)
public static void main(String[] a) {test1(); }static void test1() {// Integer的自动拆装箱的陷阱(整型数-128到127的 ...
- Integer缓存池
new Integer(127)和Integer.valueOf(127)是有区别的:理由如下! new Integer(127):每次都会创建一个新的对象. Integer.valueOf(127) ...
- Int里-128到127的学问
转:http://blog.csdn.net/chengzhezhijian/article/details/9628251 最近在项目中遇到一个问题,两个值相同的Integer型值进行==比较时,发 ...
- Integer缓存IntegerCache详解
版权声明 本文原创作者:谷哥的小弟 作者博客地址:http://blog.csdn.net/lfdfhl 引子 今天,我们从一段非常简单的代码说起. 示例代码 package cn.com; /** ...
- Integer缓存策略
先看一段代码 @Testvoid contextLoads() {Integer a=10;Integer b=10;System.out.println(a==b); //trueInteger c ...
- java字面量和符号引用_java中字面量,常量和变量之间的区别(附:Integer缓存机制)...
一.引子 在各种教科书和博客中这三者经常被引用,今天复习到内存区域,想起常量池中就是存着字面量和符号引用,其实这三者并不是只在java中才有,各个语言中都有类似的定义,所以做一下总结,以示区分. 二. ...
- 8位alu运算器(vhdl语言)_C语言中signed char类型,能表示-128到127,为什么负数多一位?...
初学者在学习C语言,谈到不同数据类型时,一般都能理解 unsigned 和 signed 的区别,无非就是有无符号而已.但是对于 signed 数据类型的数据范围,初学者却常常会感到迷惑. 对于 si ...
最新文章
- Linux 小知识翻译 - 「命令行的提示符」
- R语言效用分析 ( 效能分析、Power analysis)确定样本量、假设检验与两类错误、pwr包进行效用分析 ( 效能分析、Power analysis)的常用函数列表
- target tile component.js load logic
- 【点分治】Tree(luogu 4178/金牌导航 点分治-1)
- 键盘录入,替换敏感词
- diff算法阮一峰_【重学数据结构与算法(JS)】字符串匹配算法(三)——BM算法
- java并发编程之线程的生命周期详解
- CallStack获取函数堆栈
- 网络入门-TRANK
- react后台管理项目
- 2006年网络名言100句(转)
- Swagger之OAS 3.0配置
- python实现大规模邻域搜索(LNS)求解旅行商问题(TSP)
- 推动企业上云实施经验总结
- 搭建属于自己的数字IC EDA环境(六):开机自动激活 Synopsys license
- veins安装及运行笔记
- PowerPC PPC460-S MMU(六 Shadow TLB)
- 浅谈氢能源三大竞争优势
- VMware Workstation 安装centOS
- 物业管理信息系统的研究与实现