java == 对于整数
我发现 Integer 对象有以下 == 行为,但我无法理解它。 (我很清楚应该使用 equals 进行此类比较,但我正在学习 OCPJP...)
总之, == 对于 1000 可以按预期工作,但对于 10 则不行。
前一个代码片段是:
Integer i1 = 1000;
Integer i2 = 1000;
if(i1 != i2) System.out.println("different objects");
if(i1.equals(i2)) System.out.println("meaningfully equal");
并且它的行为正如人们所期望的:
different objects
meaningfully equal
但是后者:
Integer i3 = 10;
Integer i4 = 10;
if(i3 == i4) System.out.println("same object");
if(i3.equals(i4)) System.out.println("meaningfully equal");
具有以下输出:
same object
meaningfully equal
有人可以解释为什么会发生这种情况吗?
Possible Duplicate:
Inconsistent behavior on java's ==
Integer wrapper objects share the same instances only within the value 127?
I have found the following == behaviour for Integer objects and I fail to understand it. (I am well aware that one should use equals for such comparisons, but I am studying for OCPJP...)
On short, == works as expected for 1000, but not for 10.
The former fragment of code is:
Integer i1 = 1000;
Integer i2 = 1000;
if(i1 != i2) System.out.println("different objects");
if(i1.equals(i2)) System.out.println("meaningfully equal");
and it behaves as one would expect:
different objects
meaningfully equal
The latter though:
Integer i3 = 10;
Integer i4 = 10;
if(i3 == i4) System.out.println("same object");
if(i3.equals(i4)) System.out.println("meaningfully equal");
has the following output:
same object
meaningfully equal
Can someone please explain why this is happening?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
从 Java 5 开始,引入了包装类缓存。下面是对位于 Integer 缓存中的内部类 IntegerCache 创建的缓存的检查。例如,下面的代码将创建一个缓存:
或者
创建256个范围为-128到127的Integer对象,这些对象都存储在一个Integer数组中。这种缓存功能可以通过查看 Integer 中的内部类 IntegerCache 来了解:
因此,当使用 Integer.valueOf 创建对象或直接将值分配给 -128 到 127 范围内的 Integer 时,同一对象将被退回。因此,考虑以下示例:
输出为:
需要注意的是,对象 i 和 p 只等于 true,因为它们是同一个对象,比较不是基于值,而是基于对象相等。如果整数 i 和 p 超出 -128 或 127 的范围,则不会使用缓存,因此会创建新对象。在进行值比较时,始终使用“.equals”方法。还需要注意的是,实例化 Integer 不会创建此缓存。
请记住,“==”始终用于对象相等,它没有被重载用于比较未装箱的值
Since Java 5, wrapper class caching was introduced. The following is an examination of the cache created by an inner class, IntegerCache, located in the Integer cache. For example, the following code will create a cache:
or
256 Integer objects are created in the range of -128 to 127 which are all stored in an Integer array. This caching functionality can be seen by looking at the inner class, IntegerCache, which is found in Integer:
So when creating an object using Integer.valueOf or directly assigning a value to an Integer within the range of -128 to 127 the same object will be returned. Therefore, consider the following example:
The output is:
It is important to note that object i and p only equate to true because they are the same object, the comparison is not based on the value, it is based on object equality. If Integer i and p are outside the range of -128 or 127 the cache is not used, therefore new objects are created. When doing a comparison for value always use the “.equals” method. It is also important to note that instantiating an Integer does not create this caching.
Remember that “==” is always used for object equality, it has not been overloaded for comparing unboxed values
请参阅整数包装对象共享相同的实例仅在值 127 内?。 Integer 类为公共值保留共享静态实例。
See Integer wrapper objects share the same instances only within the value 127?. The Integer class keeps shared static instances around for common values.