比较非原始长值127和128 [英] Compare non-primitive Long values 127 and 128

查看:158
本文介绍了比较非原始长值127和128的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用如果条件比较两个对象值。当这些值小于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的值时出现问题?如果变量数据类型更改为长原语,则比较对所有情况都适用。

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.

推荐答案


为什么比较长变量和值大于127有问题?如果上述变量的数据类型是原始(long),那么代码适用于所有值。

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 em> cached ),所有引用都将指向相同的对象实例。 (N个变量,1个实例)

  • 如果将N变量设置为 128 >),你将有一个由每个引用指向的对象实例。 (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.

c $ c> Long 对象引用,而不是 long 原始值, 运算符。如果不是这个缓存机制,这些比较总是失败,所以真正的问题是比较装箱的值和 == 运算符。

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 ,始终是个好主意,

IMO, it's always a good idea to stick with .equals() methods when dealing with Object comparisons.

参考链接:

  • 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屋!

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