java == 对于整数

发布于 2025-01-06 17:51:42 字数 1026 浏览 0 评论 0原文

可能的重复:
java 上的不一致行为 ==
整数包装对象仅共享相同的实例在值 127 之内?

我发现 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

泪冰清 2025-01-13 17:51:42

从 Java 5 开始,引入了包装类缓存。下面是对位于 Integer 缓存中的内部类 IntegerCache 创建的缓存的检查。例如,下面的代码将创建一个缓存:

Integer myNumber = 10

或者

Integer myNumber = Integer.valueOf(10);

创建256个范围为-128到127的Integer对象,这些对象都存储在一个Integer数组中。这种缓存功能可以通过查看 Integer 中的内部类 IntegerCache 来了解:

因此,当使用 Integer.valueOf 创建对象或直接将值分配给 -128 到 127 范围内的 Integer 时,同一对象将被退回。因此,考虑以下示例:

Integer i = 100;
Integer p = 100;
if (i == p)  
    System.out.println("i and p are the same.");
if (i != p)
    System.out.println("i and p are different.");   
if(i.equals(p))
    System.out.println("i and p contain the same value.");

输出为:

i and p are the same.
i and p contain the same value.

需要注意的是,对象 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:

Integer myNumber = 10

or

Integer myNumber = Integer.valueOf(10);

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:

Integer i = 100;
Integer p = 100;
if (i == p)  
    System.out.println("i and p are the same.");
if (i != p)
    System.out.println("i and p are different.");   
if(i.equals(p))
    System.out.println("i and p contain the same value.");

The output is:

i and p are the same.
i and p contain the same value.

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

烟─花易冷 2025-01-13 17:51:42

请参阅整数包装对象共享相同的实例仅在值 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文