Integer value comparison

后端 未结 7 1281
北恋
北恋 2021-02-02 09:31

I\'m a newbie Java coder and I just read a variable of an integer class can be described three different ways in the API. I have the following code:

if (count.         


        
7条回答
  •  悲哀的现实
    2021-02-02 10:16

    Although you could certainly use the compareTo method on an Integer instance, it's not clear when reading the code, so you should probably avoid doing so.

    Java allows you to use autoboxing (see http://java.sun.com/j2se/1.5.0/docs/guide/language/autoboxing.html) to compare directly with an int, so you can do:

    if (count > 0) { }
    

    And the Integer instance count gets automatically converted to an int for the comparison.

    If you're having trouble understanding this, check out the link above, or imagine it's doing this:

    if (count.intValue() > 0) { }
    

提交回复
热议问题