比较Java中的Integer值,奇怪的行为 [英] Comparing Integer values in Java, strange behavior

查看:85
本文介绍了比较Java中的Integer值,奇怪的行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

和我一起走..

Integer x = 23;
Integer y = 23;

if (x == y)
    System.out.println("what else");      // All is well as expected
else
    System.out.println("...");

虽然

Integer x = someObject.getIndex();
Integer y = someOtherObject.getSomeOtherIndex();

if (x == y)
    System.out.println("what else");  
else
    System.out.println("...");        // Prints this 

嗯...我尝试转换为int

Hmm ... I try casting to int

int x = someObject.getIndex();
int y = someOtherObject.getSomeOtherIndex()

if (x == y)       
    System.out.println("what else");   // works fine
else
    System.out.println("...");  

它们都是整数吗?

System.out.println(x.getClass().getName());              // java.lang.Integer
System.out.println(y.getClass().getName());              // java.lang.Integer
System.out.println(someObject.getIndex());               // java.lang.Integer
System.out.println(someOtherObject.getSomeOtherIndex()); // java.lang.Integer

你们怎么看?什么可以解释这样的?

推荐答案

您正在比较整数值,它们是引用。你通过自动装箱提出这些参考资料。对于某些值(保证为-128到127),JRE维护一个 Integer 对象的缓存。对于更高的值,它不会。来自第5.1.7节JLS

You're comparing Integer values, which are references. You're coming up with those references via autoboxing. For some values (guaranteed for -128 to 127) the JRE maintains a cache of Integer objects. For higher values, it doesn't. From section 5.1.7 of the JLS:


如果装箱的值p为真,假,字节或范围为\\的字符u0000到\ u007f,或者在-128到127(含)之间的int或short数,然后让r1和r2成为p的任意两次装箱转换的结果。总是这样的情况是r1 == r2。

If the value p being boxed is true, false, a byte, or a char in the range \u0000 to \u007f, or an int or short number between -128 and 127 (inclusive), then let r1 and r2 be the results of any two boxing conversions of p. It is always the case that r1 == r2.

理想情况下,装箱给定的原始值p总会产生相同的参考。实际上,使用现有的实现技术可能不可行。上述规则是务实的妥协。上面的最后一个条款要求将某些常见值装入无法区分的对象中。实现可以懒惰或急切地缓存这些。对于其他值,此公式不允许对程序员的盒装值的身份进行任何假设。这将允许(但不要求)共享部分或全部这些引用。

Ideally, boxing a given primitive value p, would always yield an identical reference. In practice, this may not be feasible using existing implementation techniques. The rules above are a pragmatic compromise. The final clause above requires that certain common values always be boxed into indistinguishable objects. The implementation may cache these, lazily or eagerly. For other values, this formulation disallows any assumptions about the identity of the boxed values on the programmer's part. This would allow (but not require) sharing of some or all of these references.

这可以确保在大多数情况下,行为将是所需的行为,而不会强加不当的性能损失,特别是在小型设备上。例如,较少内存限制的实现可以缓存所有char和short值,以及-32K到+ 32K范围内的int和long值。

This ensures that in most common cases, the behavior will be the desired one, without imposing an undue performance penalty, especially on small devices. Less memory-limited implementations might, for example, cache all char and short values, as well as int and long values in the range of -32K to +32K.

道德:当你对基础 int 感兴趣时,不要比较整数引用值。使用 .equals()或首先获取 int 值。

Moral: don't compare Integer references when you're interested in the underlying int values. Use .equals() or get the int values first.

这篇关于比较Java中的Integer值,奇怪的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆