如何正确比较 Java 中的两个整数? [英] How can I properly compare two Integers in Java?

查看:36
本文介绍了如何正确比较 Java 中的两个整数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如果你将一个装箱的原始整数与一个常量进行比较,例如:

整数a = 4;如果 (a <5)

a 将自动拆箱,比较将起作用.

但是,当您比较两个装箱的 Integers 并且想要比较相等或小于/大于时会发生什么?

整数a = 4;整数 b = 5;如果 (a == b)

上面的代码会检查它们是否是同一个对象,还是会自动拆箱?

关于:

整数a = 4;整数 b = 5;如果 (a < b)

?

解决方案

No, == between Integer, Long etc将检查引用相等 - 即

整数 x = ...;整数 y = ...;System.out.println(x == y);

这将检查xy 是否引用相同对象 而不是相等 对象.

所以

Integer x = new Integer(10);整数 y = 新整数(10);System.out.println(x == y);

保证打印false.小"自动装箱值的实习可能会导致棘手的结果:

整数 x = 10;整数 y = 10;System.out.println(x == y);

由于拳击规则,这将打印 true (JLS 第 5.1.7 节).它仍然使用引用相等,但引用真正相等.

<块引用>

如果被装箱的值 p 是介于-128 和 127(包括第 3.10.1 节),或布尔文字 true 或 false(第 3.10.3 节),或介于 'u0000' 和 'u007f' 之间的字符文字包含(第 3.10.4 节),然后让 a 和 b 是任何两个装箱的结果p. 的转换a == b 总是这样.

我个人会使用:

if (x.intValue() == y.intValue())

if (x.equals(y))

如您所说,对于包装类型(IntegerLong 等)和数字类型(int>long 等)包装器类型值被取消装箱并且测试应用于所涉及的原始值.

这是作为二进制数字提升的一部分发生的 (JLS 第 5.6.2 节).查看每个操作员的文档,看看它是否被应用.例如,来自 ==!= 的文档 (JLS 15.21.1):

<块引用>

如果等式的操作数运算符都是数字类型,或者一个是数字类型,另一个是可转换(第 5.1.8 节)为数字类型,二进制数字提升是对操作数执行(第 5.6.2 节).

<<=>>= (JLS 15.20.1)

<块引用>

a 的每个操作数的类型数值比较运算符必须是可转换(第 5.1.8 节)到的类型原始数字类型,或发生编译时错误.二进制数值提升是在操作数(第 5.6.2 节).如果被提升操作数的类型是 int 或 long,那么有符号整数比较是执行;如果这个提升的类型是float 或 double,然后是浮点数进行比较.

请注意,在 类型都不是数字类型的情况下,这些都不会被视为一部分.

I know that if you compare a boxed primitive Integer with a constant such as:

Integer a = 4;
if (a < 5)

a will automatically be unboxed and the comparison will work.

However, what happens when you are comparing two boxed Integers and want to compare either equality or less than/greater than?

Integer a = 4;
Integer b = 5;

if (a == b)

Will the above code result in checking to see if they are the same object, or will it auto-unbox in that case?

What about:

Integer a = 4;
Integer b = 5;

if (a < b)

?

解决方案

No, == between Integer, Long etc will check for reference equality - i.e.

Integer x = ...;
Integer y = ...;

System.out.println(x == y);

this will check whether x and y refer to the same object rather than equal objects.

So

Integer x = new Integer(10);
Integer y = new Integer(10);

System.out.println(x == y);

is guaranteed to print false. Interning of "small" autoboxed values can lead to tricky results:

Integer x = 10;
Integer y = 10;

System.out.println(x == y);

This will print true, due to the rules of boxing (JLS section 5.1.7). It's still reference equality being used, but the references genuinely are equal.

If the value p being boxed is an integer literal of type int between -128 and 127 inclusive (§3.10.1), or the boolean literal true or false (§3.10.3), or a character literal between 'u0000' and 'u007f' inclusive (§3.10.4), then let a and b be the results of any two boxing conversions of p. It is always the case that a == b.

Personally I'd use:

if (x.intValue() == y.intValue())

or

if (x.equals(y))

As you say, for any comparison between a wrapper type (Integer, Long etc) and a numeric type (int, long etc) the wrapper type value is unboxed and the test is applied to the primitive values involved.

This occurs as part of binary numeric promotion (JLS section 5.6.2). Look at each individual operator's documentation to see whether it's applied. For example, from the docs for == and != (JLS 15.21.1):

If the operands of an equality operator are both of numeric type, or one is of numeric type and the other is convertible (§5.1.8) to numeric type, binary numeric promotion is performed on the operands (§5.6.2).

and for <, <=, > and >= (JLS 15.20.1)

The type of each of the operands of a numerical comparison operator must be a type that is convertible (§5.1.8) to a primitive numeric type, or a compile-time error occurs. Binary numeric promotion is performed on the operands (§5.6.2). If the promoted type of the operands is int or long, then signed integer comparison is performed; if this promoted type is float or double, then floating-point comparison is performed.

Note how none of this is considered as part of the situation where neither type is a numeric type.

这篇关于如何正确比较 Java 中的两个整数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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