Java Integer Cache
Java Integer Cache
Java 代码
public class IntegerDemo {
public static void main(String[] args) {
Integer a = new Integer(1);
Integer b = 1;
int c = 1;
System.out.println(a == b);
System.out.println(a == c);
System.out.println(b == c);
Integer f1 = 100, f2 = 100, f3 = 150, f4 = 150;
System.out.println(f1 == f2);
System.out.println(f3 == f4);
}
}
运行结果
false
true
true
true
false
```
反编译代码
public class IntegerDemo {
public static void main(String[] args) {
Integer a = new Integer(1);
Integer b = Integer.valueOf(1);
int c = 1;
System.out.println(a == b);
System.out.println(a.intValue() == c);
System.out.println(b.intValue() == c);
Integer f1 = Integer.valueOf(100);
Integer f2 = Integer.valueOf(100);
Integer f3 = Integer.valueOf(150);
Integer f4 = Integer.valueOf(150);
System.out.println(f1 == f2);
System.out.println(f3 == f4);
}
}
</div>
```java
public class Test {
public static void main(String[] args) {
Integer a = 1;
Integer b = 2;
Integer c = 3;
Integer d = 3;
Integer e = 321;
Integer f = 321;
Long g = 3L;
System.out.println(c == d);
System.out.println(e == f);
System.out.println(c == (a + b));
System.out.println(c.equals(a + b));
System.out.println(g == (a + b));
System.out.println(g.equals(a + b));
}
}
运行结果
true
false
true
true
true
false
```
反编译代码
public class Test {
public static void main(String[] args) {
Integer a = Integer.valueOf(1);
Integer b = Integer.valueOf(2);
Integer c = Integer.valueOf(3);
Integer d = Integer.valueOf(3);
Integer e = Integer.valueOf(321);
Integer f = Integer.valueOf(321);
Long g = Long.valueOf(3L);
System.out.println(c == d);
System.out.println(e == f);
System.out.println(c.intValue() == a.intValue() + b.intValue());
System.out.println(c.equals(Integer.valueOf(a.intValue() + b.intValue())));
System.out.println(g.longValue() == a.intValue() + b.intValue());
System.out.println(g.equals(Integer.valueOf(a.intValue() + b.intValue())));
}
}
```
解析
1、Byte,Short,Integer,Long 的缓存范围为[-128,127],Character 的缓存范围为[0,127]。
2、int 和 int 之间用 == 比较,肯定为 true。另外基本数据类型没有 equals 方法。
3、int 和 Integer 之间比较,Integer 会自动拆箱,== 和 equals 肯定都为 true。
4、int 和 new Integer 之间比较,Integer 会自动拆箱,调用 intValue 方法,所以 == 和 equals 肯定都为 true。
5、Integer 和 Integer 之间比较的时候,由于直接赋值的话会进行自动装箱。所以当值在[-128,127] 中的时候,由于值缓存在 IntegerCache 中,那么当赋值在这个区间的时候,不会创建新的 Integer 对象,而是直接从缓存中获取已经创建好的 Integer 对象。而当大于这个区间的时候,会直接 new Integer。
6、当 Integer 和 Integer 之间进行 == 比较的时候,在[-128,127] 区间的时候,为 true。不在这个区间,则为 false。
7、当 Integer 和 Integer 之间进行 equals 比较的时候,由于 Integer 的 equals 方法进行了重写,比较的是内容,所以为 true。
8、Integer 和 new Integer 之间比较:new Integer 会创建对象,存储在堆中。而 Integer 在[-128,127] 中,从缓存中取,否则会 new Integer。所以 Integer 和 new Integer 进行 == 比较的话,肯定为 false ;Integer 和 new Integer 进行 equals 比较的话,肯定为 true。
9、new Integer 和 new Integer 进行 == 比较的时候,肯定为 false ;进行 equals 比较的时候,肯定为 true。原因是 new 的时候,会在堆中创建对象,分配的地址不同,== 比较的是内存地址,所以肯定不同。
10、装箱过程是通过调用包装类的 valueOf 方法实现的。
11、拆箱过程是通过调用包装类的 xxxValue 方法实现的(xxx 表示对应的基本数据类型)。
参考资料
Java Integer Cache的更多相关文章
- 【转】理解Java Integer的缓存策略
本文将介绍 Java 中 Integer 缓存的相关知识.这是 Java 5 中引入的一个有助于节省内存.提高性能的特性.首先看一个使用 Integer 的示例代码,展示了 Integer 的缓存行为 ...
- Java Integer(-128~127)值的==和equals比较产生的思考
最近在项目中遇到一个问题,两个值相同的Integer型值进行==比较时,发现Integer其中的一些奥秘,顺便也复习一下==和equals的区别,先通过Damo代码解释如下: System.out.p ...
- 理解Java Integer的缓存策略
转载自http://www.importnew.com/18884.html 本文将介绍 Java 中 Integer 缓存的相关知识.这是 Java 5 中引入的一个有助于节省内存.提高性能的特性. ...
- java integer对象判断两个数字是否相等
java integer对象判断两个数字是否相等,不一定对 问题发生的背景:javaweb的项目,起先,因为在java中实体类中的int类型在对象初始化之后会给int类型的数据默认赋值为0,这样在很多 ...
- Java Integer类分析
public static final int MIN_VALUE = 0x80000000; -2^31 public static final int MAX_VALUE = 0x7ff ...
- 理解Java Integer的缓存策略【转】
本文由 ImportNew - 挖坑的张师傅 翻译自 javapapers.欢迎加入翻译小组.转载请见文末要求. 本文将介绍 Java 中 Integer 缓存的相关知识.这是 Java 5 中引入的 ...
- Java Integer类型比较
今天做了一道题目题目如下: Integer a=10; Integer b=10; System.out.print(a==b); Integer c=200; Integer d=200; Syst ...
- java --Integer 学习
本文版权归 远方的风lyh和博客园共有,欢迎转载,但须保留此段声明,并给出原文链接,谢谢合作. 在网上看到一个面试题,没有完全做, 本代码基于JDK8 //下面代码运行结果是 public class ...
- java Integer.valueOf 和 Integer.parseInt 和 new Integer区别及注意事项
先看一下下面的结果 1.System.out.println(127==127); //true , int type compare 2.System.out.println(128==128); ...
随机推荐
- STL源码分析-iterator
http://note.youdao.com/noteshare?id=4efcb6441063dae956c226f91c161897
- bzoj 2795 [Poi2012]A Horrible Poem hash+数论
2795: [Poi2012]A Horrible Poem Time Limit: 50 Sec Memory Limit: 128 MBSubmit: 640 Solved: 322[Subm ...
- linux shell学习一
本博客参考自: http://www.cnblogs.com/waitig/p/5523409.html <shell从入门到精通> 张春晓编著 Shell简介 Shell自身是一个用C ...
- 「LibreOJ β Round #4」游戏
https://loj.ac/problem/524 题目描述 qmqmqm和sublinekelzrip要进行一场游戏,其规则是这样的: 首先有一个序列,其中每个位置是一个整数或是X.双方轮流将X的 ...
- HDU 6211 卡常数取模 预处理 数论
求所有不超过1e9的 primitive Pythagorean triple中第2大的数取模$2^k$作为下标,对应a[i]数组的和. 先上WIKI:https://en.wikipedia.org ...
- Python print "hello world" SyntaxError: invalid syntax
刚安装Python,在IDLE中输入print “Hello World”,谁知却发生错误: >>> print "Hello World"SyntaxError ...
- 详解ASP.NET4 GridView的四种排序样式
与ASP.NET 的其他Web控件一能够,Gridview控件拥有很多不同的CSS样式属性设置,包括象CssClass,Font字体,ForeColor,BackColor,BackColor, Wi ...
- DOM使用
DOM树模型 document |-html |-head |-.... |-body |-..... 要解析页面的前提是要拿到一个对象,然后利用树之间前后的关系进行对象的遍历和操作. 在DHTML的 ...
- [BZOJ 3039&洛谷P4147]玉蟾宫 题解(单调栈)
[BZOJ 3039&洛谷P4147]玉蟾宫 Description 有一天,小猫rainbow和freda来到了湘西张家界的天门山玉蟾宫,玉蟾宫宫主蓝兔盛情地款待了它们,并赐予它们一片土地. ...
- hdu 2063 过山车 二分匹配(匈牙利算法)
简单题hdu2063 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2063 过山车 Time Limit: 1000/1000 MS (Java/Ot ...