Integer 的小知识
来源:互联网 发布:实现即时通讯的php文件 编辑:程序博客网 时间:2024/06/06 14:21
先看代码
public class IntegerTest{ public static void main(String[] args) { Integer a1 = 12; Integer a2 = 12; //#1 System.out.println(a1 == a2 ); //true Integer b1 = 128; Integer b2 = 128; //#2 System.out.println(b1 == b2 ); //false //#3 Integer c1 = 12 ; int c2 = 12 ; System.out.println(c1 == c2 ); //true //#4 Integer d1 = new Integer(12); Integer d2 = new Integer(12); System.out.println(d1 == d2 ); //false }}//#1和#2 的结果不相同的原因是啥了?a. Integer a =12 ; java 反编译的时候,成为了 Integer a = Integer.valueOf(12);public static Integer valueOf(int i) { if (i >= IntegerCache.low && i <= IntegerCache.high) return IntegerCache.cache[i + (-IntegerCache.low)]; return new Integer(i); }当 i 值为[-128,127]的时候 从IntegerCache中取值。 private static class IntegerCache { static final int low = -128; static final int high; static final Integer cache[]; static { // high value may be configured by property int h = 127; String integerCacheHighPropValue = sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high"); if (integerCacheHighPropValue != null) { try { int i = parseInt(integerCacheHighPropValue); i = Math.max(i, 127); // Maximum array size is Integer.MAX_VALUE h = Math.min(i, Integer.MAX_VALUE - (-low) -1); } catch( NumberFormatException nfe) { // If the property cannot be parsed into an int, ignore it. } } high = h; cache = new Integer[(high - low) + 1]; int j = low; for(int k = 0; k < cache.length; k++) cache[k] = new Integer(j++); // range [-128, 127] must be interned (JLS7 5.1.7) assert IntegerCache.high >= 127; } private IntegerCache() {} } 看完IntegerCache类 就知道是啥事了。 //那为啥#4是false了? 因为通过构造函数形成的对象,不会从IntegerCache数组中取实例。 //#3 是从JDSK1.5开始 Integer和int比较的时候 Integer自动拆箱,再进行比较。
0 0
- Integer 的小知识
- 关于Integer对象的一些小知识
- Integer小知识(新的理解)
- 关于Integer自动装箱和拆箱的小知识
- int自动封装Integer的小知识(转载)
- 关于Integer的知识
- 关于Integer的一些知识
- 【C/C++】鲜为人知的知识--Integer Promotion
- 超级链接的小知识
- 自己的小知识
- 有用的小知识
- 琐碎的小知识
- List的小知识
- 备忘的小知识
- js的小知识
- 回车键的小知识
- shell 的小知识
- mysql的小知识
- 循环往list中add对象却总是add的是一个对象
- 数据结构-Java实现线性表
- Log4j日志记录器
- 判断字段在哪些表
- Apache Kylin的快速数据立方体算法——概述
- Integer 的小知识
- Spring beans架构--set注入
- Angular2学习之懒加载(Lazy)路由(router)问题
- Gradle学习笔记(三)java项目编译
- 瀑布流封装
- 安卓学习笔记-3
- 读《聪明人用方格笔记本》有图了有内容了
- mysql-5.5.33编译安装
- 学习python的第二十四天-Web