这个问题在这里已经有了答案:
如何正确比较Java中的两个整数? 7个答案我知道你不能使用==或!=来比较数值对象的值,而必须使用.equals()。 但经过大量搜索后,我无法找到关于是否可以使用其他比较运算符的声明,除了使用.compare()或.compareTo()的建议之外,这些建议感觉效率低下,因为它们需要两个比较:a至b,然后将结果归零。
尽管==和!=比较对象的地址,但其他比较运算符似乎会比较数值。 例如下面的代码片段:
Integer a = new Integer(3000); Integer b = new Integer(3000); System.out.println("a < b " + (a < b)); System.out.println("a <= b " + (a <= b)); System.out.println("a == b " + (a == b)); System.out.println("a >= b " + (a >= b)); System.out.println("a > b " + (a > b));产生
a < b false a <= b true a == b false a >= b true a > b false这似乎表明所有的操作符,但==比较值,而不是对象的地址。 它被接受的用法是使用<=运算符类,还是仅仅是我的编译器的某个不受支持的功能?
This question already has an answer here:
How to properly compare two Integers in Java? 8 answersI understand that you can't use == or != to compare values of numeric objects and have to use .equals() instead. But after a fair amount of searching I haven't been able to find a statement about whether or not you can use the other comparison operators, other than suggestions to use .compare() or .compareTo() which feel inefficient because they require two comparisons: a to b, then the result of that to zero.
Despite == and != comparing the addresses of the objects, the other comparison operators appear to compare the numeric values. For instance the following code snippet:
Integer a = new Integer(3000); Integer b = new Integer(3000); System.out.println("a < b " + (a < b)); System.out.println("a <= b " + (a <= b)); System.out.println("a == b " + (a == b)); System.out.println("a >= b " + (a >= b)); System.out.println("a > b " + (a > b));produces
a < b false a <= b true a == b false a >= b true a > b falseWhich appears to indicate all operators but == compare the value, not the address of the object. Is it accepted usage to use the <= class of operators, or just an unsupported feature of my compiler or something?
最满意答案
是的,但请注意Integer是对象 ,而不是基本的int 。 > , < , >=和<=运算符的用法不是用于对象,而是用于基元,因此当使用其中任何一个时, Integer被自动写入int 。 在对象中使用== ,您正在比较它们的引用。 使用equals来比较它们。
不过请注意, Integer类有一个缓存,用于存储从-128到127 Integer引用。 这意味着如果你这样做:
Integer i1 = 127; Integer i2 = 127; System.out.println(i1 == i2);将打印true 。
Yes, but note that Integer are objects, not primitive int. Usage of >, <, >= and <= operators are not meant for objects, but for primitives, so when using any of these, the Integer is autoboxed to int. While when using == in objects you are comparing their references. Use equals instead to compare them.
Still, note that Integer class has an cache that store Integer references from -128 to 127. This means that if you do this:
Integer i1 = 127; Integer i2 = 127; System.out.println(i1 == i2);Will print true.
do <=和> =关系运算符使用Integer对象[duplicate](do <= and >= relational operators work with Integer objects [duplicate])这个问题在这里已经有了答案:
如何正确比较Java中的两个整数? 7个答案我知道你不能使用==或!=来比较数值对象的值,而必须使用.equals()。 但经过大量搜索后,我无法找到关于是否可以使用其他比较运算符的声明,除了使用.compare()或.compareTo()的建议之外,这些建议感觉效率低下,因为它们需要两个比较:a至b,然后将结果归零。
尽管==和!=比较对象的地址,但其他比较运算符似乎会比较数值。 例如下面的代码片段:
Integer a = new Integer(3000); Integer b = new Integer(3000); System.out.println("a < b " + (a < b)); System.out.println("a <= b " + (a <= b)); System.out.println("a == b " + (a == b)); System.out.println("a >= b " + (a >= b)); System.out.println("a > b " + (a > b));产生
a < b false a <= b true a == b false a >= b true a > b false这似乎表明所有的操作符,但==比较值,而不是对象的地址。 它被接受的用法是使用<=运算符类,还是仅仅是我的编译器的某个不受支持的功能?
This question already has an answer here:
How to properly compare two Integers in Java? 8 answersI understand that you can't use == or != to compare values of numeric objects and have to use .equals() instead. But after a fair amount of searching I haven't been able to find a statement about whether or not you can use the other comparison operators, other than suggestions to use .compare() or .compareTo() which feel inefficient because they require two comparisons: a to b, then the result of that to zero.
Despite == and != comparing the addresses of the objects, the other comparison operators appear to compare the numeric values. For instance the following code snippet:
Integer a = new Integer(3000); Integer b = new Integer(3000); System.out.println("a < b " + (a < b)); System.out.println("a <= b " + (a <= b)); System.out.println("a == b " + (a == b)); System.out.println("a >= b " + (a >= b)); System.out.println("a > b " + (a > b));produces
a < b false a <= b true a == b false a >= b true a > b falseWhich appears to indicate all operators but == compare the value, not the address of the object. Is it accepted usage to use the <= class of operators, or just an unsupported feature of my compiler or something?
最满意答案
是的,但请注意Integer是对象 ,而不是基本的int 。 > , < , >=和<=运算符的用法不是用于对象,而是用于基元,因此当使用其中任何一个时, Integer被自动写入int 。 在对象中使用== ,您正在比较它们的引用。 使用equals来比较它们。
不过请注意, Integer类有一个缓存,用于存储从-128到127 Integer引用。 这意味着如果你这样做:
Integer i1 = 127; Integer i2 = 127; System.out.println(i1 == i2);将打印true 。
Yes, but note that Integer are objects, not primitive int. Usage of >, <, >= and <= operators are not meant for objects, but for primitives, so when using any of these, the Integer is autoboxed to int. While when using == in objects you are comparing their references. Use equals instead to compare them.
Still, note that Integer class has an cache that store Integer references from -128 to 127. This means that if you do this:
Integer i1 = 127; Integer i2 = 127; System.out.println(i1 == i2);Will print true.
发布评论