比较加框的长值127和128 [英] Comparing boxed Long values 127 and 128
问题描述
我想使用 if
条件来比较两个 Long 对象值。当这些值小于128 时, if
条件正常工作,但当大于或等于128 ,比较失败。
I want to compare two Long objects values using if
conditions. When these values are less than 128, the if
condition works properly, but when they are greater than or equal to 128, comparison fails.
示例:
Long num1 = 127;
Long num2 = 127;
if (num1 == num2) {
// Works ok
}
上述代码的比较正常,但在以下代码中失败:
Comparison on the code above works properly, but fails in the code below:
Long num1 = 128;
Long num2 = 128;
if (num1 == num2) {
// Does NOT work
}
为什么在长变量与大于127 的值进行比较时会出现问题?如果变量数据类型更改为 long primitives ,则比较适用于所有情况。
Why is there a problem in comparing Long variables with values greater than 127? If the variables data types are changed to long primitives, then the comparisons work for all cases.
推荐答案
TL; DR
Java缓存从 -128
到<$的整数盒装实例C $ C> 127 。由于您使用 ==
来比较对象引用而不是值,因此只有缓存的对象才匹配。 Ether使用 long
未装箱的原始值或使用 .equals()
来比较 Long
对象。
Java caches Integer boxed instances from -128
to 127
. Since you are using ==
to compare objects references instead of values, only cached objects will match. Ether work with long
unboxed primitive values or use .equals()
to compare your Long
objects.
长(双关语)版
为什么比较Long变量和大于127的值会有问题?如果上述变量的数据类型是原始的(长),则代码适用于所有值。
Why there is problem in comparing Long variable with value greater than 127? If the data type of above variable is primitive (long) then code work for all values.
Java缓存范围-128到127 的整数对象实例。这就是说:
Java caches Integer objects instances from the range -128 to 127. That said:
- 如果设置为N Long变量,则值
127
( cached ),所有引用都会指向相同的对象实例。 (N个变量,1个实例) - 如果设置为N Long变量,则值
128
( not cached ),每个引用都会指向一个对象实例。 (N个变量,N个实例)
- If you set to N Long variables the value
127
(cached), the same object instance will be pointed by all references. (N variables, 1 instance) - If you set to N Long variables the value
128
(not cached), you will have an object instance pointed by every reference. (N variables, N instances)
这就是为什么:
Long val1 = 127L;
Long val2 = 127L;
System.out.println(val1 == val2);
Long val3 = 128L;
Long val4 = 128L;
System.out.println(val3 == val4);
输出:
true
false
true
false
对于 127L 值,因为两者都是引用(val1和val2)指向内存中的相同对象实例(缓存),它返回 true
。
For the 127L value, since both references (val1 and val2) point to the same object instance in memory (cached), it returns true
.
另一方面,对于 128 值,由于没有在内存中缓存的实例,因此为盒装值的任何新分配创建一个新实例,从而产生两个不同的实例(由val3指向)和val4)并返回 false
对它们进行比较。
On the other hand, for the 128 value, since there is no instance for it cached in memory, a new one is created for any new assignments for boxed values, resulting in two different instances (pointed by val3 and val4) and returning false
on the comparison between them.
这只是因为你比较两个长
对象引用,而不是 long
原始值, ==
运营商。如果不是这个Cache机制,那么这些比较总是失败,所以这里真正的问题是将盒装值与 ==
运算符进行比较。
That happens solely because you are comparing two Long
object references, not long
primitive values, with the ==
operator. If it wasn't for this Cache mechanism, these comparisons would always fail, so the real problem here is comparing boxed values with ==
operator.
将这些变量更改为原始 long
类型可以防止这种情况发生,但是如果您需要保留代码使用 Long
对象,您可以安全地使用以下方法进行这些比较:
Changing these variables to primitive long
types will prevent this from happening, but in case you need to keep your code using Long
objects, you can safely make these comparisons with the following approaches:
System.out.println(val3.equals(val4)); // true
System.out.println(val3.longValue() == val4.longValue()); // true
System.out.println((long)val3 == (long)val4); // true
IMO ,坚持使用<始终是一个好主意处理对象比较时强> .equals()方法。
免责声明:如果这些值中的任何一个,大多数比较都会失败为null(即使转换为long也会抛出异常),因此需要额外的检查以适应这些情况。
Disclaimer: Most comparisons will fail if any of these values are null (even casting to long will throw an exception), so additional checkings are needed to accomodate these scenarios.
参考链接:
- https://today.java.net/pub/a/today/2005/03/24/autoboxing.html
- https://blogs.oracle.com/darcy/entry/boxing_and_caches_integer_valueof
- < a href =http://java.dzone.com/articles/surprising-results-autoboxing =nofollow noreferrer> http://java.dzone.com/articles/surprising-results-autoboxing
- https://today.java.net/pub/a/today/2005/03/24/autoboxing.html
- https://blogs.oracle.com/darcy/entry/boxing_and_caches_integer_valueof
- http://java.dzone.com/articles/surprising-results-autoboxing
这篇关于比较加框的长值127和128的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!