首先看一段代码(使用JDK 5),如下:
public class Hello { public static void main(String[] args) { int a = 1000, b = 1000; System.out.println(a == b); Integer c = 1000, d = 1000; System.out.println(c == d); Integer e = 100, f = 100; System.out.println(e == f); } }
输出结果:
true false true
为了节省内存,对于下列包装对象的两个实例,当它们的基本值相同时,他们总是==:
Boolean Byte Character, \u0000 - \u007f(7f是十进制的127) Integer, -128 — 127
查看jdk源码,如下:
/** * Cache to support the object identity semantics of autoboxing for values between * -128 and 127 (inclusive) as required by JLS. * * The cache is initialized on first usage. During VM initialization the * getAndRemoveCacheProperties method may be used to get and remove any system * properites that configure the cache size. At this time, the size of the * cache may be controlled by the vm option -XX:AutoBoxCacheMax=<size>. */ // value of java.lang.Integer.IntegerCache.high property (obtained during VM init) private static String integerCacheHighPropValue; static void getAndRemoveCacheProperties() { if (!sun.misc.VM.isBooted()) { Properties props = System.getProperties(); integerCacheHighPropValue = (String)props.remove("java.lang.Integer.IntegerCache.high"); if (integerCacheHighPropValue != null) System.setProperties(props); // remove from system props } } 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() {} } /** * 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); }
IntegerCache有一个静态的Integer数组,在类加载时就将-128 到 127 的Integer对象创建了,并保存在cache数组中,一旦程序调用valueOf 方法,如果i的值是在-128 到 127 之间就直接在cache缓存数组中去取Integer对象。
再看其它的包装器:
- Boolean:(全部缓存)
- Byte:(全部缓存)
- Character(<= 127缓存)
- Short(-128 — 127缓存)
- Long(-128 — 127缓存)
- Float(没有缓存)
- Doulbe(没有缓存)
相关推荐
项目里遇到的小问题的 整理
存在下述例子,判断其输出结果。 public class Main{ public static void main(String[] agrs){ Integer a = 110;...我们知道,在Java中存在基本数据类型和引用数据类型,而Integer作为int的封装类,Integer
主要介绍了java 整型数与Integer的缓存深入理解的相关资料,需要的朋友可以参考下
随手写了一个TreeNode,顺道实现了个对应的内存缓存框架,适用于数据量不大,更新也少但是反复要读的数据,比如模块信息/组织结构/频道栏目/权限等。 PS:Java是可以直接操作内存的,只是现在的框架整合让人退化了。...
java设计模式【之】享元模式【源码】【场景:多缓存可选模式】 /** * 享元模式(预加载单例) * “元” 理解为 “同一地址对象” * 运用共享技术,减少对象的创建,降低对象的数量,降低内存消耗,提高性能 * ...
(1)使用句柄:如果使用句柄访问方式, Java 堆中将会划分出一块内存来作为句柄池,reference 中存储的就是对象的句柄地址,而句柄中包含了对象实例数据
本文将介绍Java中Integer的缓存相关知识。这是在Java 5中引入的一个有助于节省内存、提高性能的功能。首先看一个使用Integer的示例代码,从中学习其缓存行为。接着我们将为什么这么实现以及他到底是如何实现的。你能...
6.4.2 constant_integer_info表 6.4.3 constant_float_info表 6.4.4 constant_long_info表 6.4.5 constant_double_info表 6.4.6 constant_class_info表 6.4.7 constant_string_info表 6.4.8 ...
1、Integer缓存池范围-128~127都是同一个地址,在缓存池范围内赋值不会创建新的对象,且不开辟新内存空间。该缓存池由源码Integer.class中的IntegerCache这个私有静态内部类定义。该缓存池与jvm关系是:缓存池创建...
其中,讨论了Java为什么不支持多继承、==和equals的区别、方法重载的条件、String为何设计成不可变、包装类的应用场景、Integer的高速缓存机制等多个方面。 通过面试题的逐一解答,读者可以了解到Java语言的一些...
7.java 中操作字符串都有哪些类?它们之间有什么区别? 8.String str="i"与 String str=new String(“i”)一样吗? 9.如何将字符串反转? 10.String 类的常用方法都有那些? 11.抽象类必须要有抽象方法吗? 12.普通...
6.4.2 CONSTANT_Integer_info表 6.4.3 CONSTANT_Float_info表 6.4.4 CONSTANT_Long_info表 6.4.5 CONSTANT_Double_info表 6.4.6 CONSTANT_Class_info表 6.4.7 CONSTANT_String_info表 6.4.8 ...
Icarus是一个可扩展的缓存框架,旨在通过仅添加一些注释来提高新的或现有的性能。 为什么要使用它? 没有任何框架的纯Java代码: final Map< Integer> cache = Maps . newHashMap(); int sum( final int . .. ...
详解Integer的享元缓存机制 JDK探险 | 详解Integer的缓存机制 JDK探秘 | ArrayList的扩容机制 JDK探秘 | String究竟在说什么(上) JDK探秘 | String究竟在说什么(下) JDK探秘 | StringBuilder 究竟有什么用 程序...
37. 一个".java"源文件中是否可以包括多个类(不是内部类)?有什么限制? 18 38. 比较truncate和delete 命令 18 39. 解释$ORACLE_HOME 和$ORACLE_BASE的区别? 19 40. session与cookie的区别和联系? 19 41. ...
1、一个".java"源文件中是否可以包括多个类(不是内部类)?有什么限制? 7 2、Java有没有goto? 7 3、说说&和&&的区别。 8 4、在JAVA中如何跳出当前的多重嵌套循环? 8 5、switch语句能否作用在byte上,能否作用在...
1、一个".java"源文件中是否可以包括多个类(不是内部类)?有什么限制? 7 2、Java有没有goto? 7 3、说说&和&&的区别。 8 4、在JAVA中如何跳出当前的多重嵌套循环? 8 5、switch语句能否作用在byte上,能否作用在...
42、一个“.java”源文件中是否可以包含多个类(不是内部类)?有什么限制? 12 43、说出一些常用的类,包,接口,请各举5 个。 12 44、Anonymous Inner Class (匿名内部类) 是否可以extends(继承)其它类?是否可以...
6.4.2 CONSTANT_Integer_info表 6.4.3 CONSTANT_Float_info表 6.4.4 CONSTANT_Long_info表 6.4.5 CONSTANT_Double_info表 6.4.6 CONSTANT_Class_info表 6.4.7 CONSTANT_String_info表 6.4.8 CONSTANT_...