Java 比较整数对象与 int

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4036167/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 10:48:31  来源:igfitidea点击:

Comparing Integer objects vs int

java

提问by Trevor

I fixed an endless loop by changing Integer to int in the following:

我通过在下面将 Integer 更改为 int 来修复无限循环:

public class IntTest {
    public static void main(String[] args) {
        Integer x=-1;
        Integer total=1000;

        while(x != total){
            System.out.println("x =" + x + "total ="+ total);
            x++;
        }
    }
}

What is the proper reason for this? I figured Integer would compare no problem.

这样做的正确原因是什么?我认为 Integer 会比较没有问题。

Thanks.

谢谢。

采纳答案by Vladimir Ivanov

Because when you make != comparing on the object it compares the references. And the references between two objects in general case are different.

因为当您对对象进行 != 比较时,它会比较引用。并且一般情况下两个对象之间的引用是不同的。

When you compare ints it always compares primitives, lets say not references( there are no objects ), but the values.

当您比较整数时,它总是比较基元,可以说不是引用(没有对象),而是值。

So, if you want to work with Integer you must use equals() on them.

因此,如果您想使用 Integer,则必须对它们使用 equals()。

Additionally, if your values are between 0 and 255 the comparison between Integer works fine, because of caching.

此外,如果您的值介于 0 和 255 之间,则由于缓存,Integer 之间的比较可以正常工作。

You can read here: http://download.oracle.com/javase/tutorial/java/data/numberclasses.html

您可以在这里阅读:http: //download.oracle.com/javase/tutorial/java/data/numberclasses.html

回答by Karel Petranek

The problem is that Integer is a class and therefore even comparison is done like for any other class - using the .equals() method. If you compare it using ==, you compare the references which are always different for two distinct instances. The primitive type intis not a class but a Java built-in type and the comparison is thus handled specially by the compiler and works as expected.

问题是 Integer 是一个类,因此甚至可以像任何其他类一样进行比较 - 使用 .equals() 方法。如果使用 == 进行比较,则比较两个不同实例的引用总是不同的。原始类型int不是类而是 Java 内置类型,因此比较由编译器专门处理并按预期工作。

回答by Valchris

Integer is a class wrapper around the Java primitive type int. They are not the same thing. you should be using int instead of Integer unless you have a valid reason (such as ArrayList<Integer> list;

Integer 是 Java 原始类型 int 的类包装器。它们不是同一件事。您应该使用 int 而不是 Integer ,除非您有正当理由(例如ArrayList<Integer> list;

回答by Bozho

Integeris an Object, and objects are compared with .equals(..)

Integer是一个Object,并且对象与.equals(..)

Only primitives are compared with ==

只比较基元 ==

That's the rule, apart from some exceptional cases, where ==canbe used for comparing objects. But even then it is not advisable.

这是规则,除了一些例外情况,==可以用于比较对象。但即便如此,这也是不可取的。

回答by Michael K

You can use Integer.intValue() to get the int value for comparison, if you truly need to use Integer at all.

如果您真的需要使用 Integer,您可以使用 Integer.intValue() 来获取用于比较的 int 值。