Integer value comparison

后端 未结 7 1267
北恋
北恋 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 09:55

    It's better to avoid unnecessary autoboxing for 2 reasons.

    For one thing, it's a bit slower than int < int, as you're (sometimes) creating an extra object;

    void doSomethingWith(Integer integerObject){ ...
      int i = 1000;
      doSomethingWith(i);//gets compiled into doSomethingWith(Integer.valueOf(i));
    

    The bigger issue is that hidden autoboxing can hide exceptions:

    void doSomethingWith (Integer count){
      if (count>0)  // gets compiled into count.intValue()>0
    

    Calling this method with null will throw a NullPointerException.

    The split between primitives and wrapper objects in java was always described as a kludge for speed. Autoboxing almost hides this, but not quite - it's cleaner just to keep track of the type. So if you've got an Integer object, you can just call compare() or intValue(), and if you've got the primitive just check the value directly.

    0 讨论(0)
  • 2021-02-02 10:11

    well i might be late on this but i would like to share something:

    Given the input: System.out.println(isGreaterThanZero(-1));

    public static boolean isGreaterThanZero(Integer value) {
        return value == null?false:value.compareTo(0) > 0;
    }
    

    Returns false

    public static boolean isGreaterThanZero(Integer value) {
        return value == null?false:value.intValue() > 0;
    }
    

    Returns true So i think in yourcase 'compareTo' will be more accurate.

    0 讨论(0)
  • 2021-02-02 10:14

    Integers are autounboxed, so you can just do

    if (count > 0) {
        .... 
    }
    
    0 讨论(0)
  • 2021-02-02 10:14

    You can also use equals:

     Integer a = 0;
    
     if (a.equals(0)) {
         // a == 0
     }
    

    which is equivalent to:

     if (a.intValue() == 0) {
         // a == 0
     }
    

    and also:

     if (a == 0) {
    
     }
    

    (the Java compiler automatically adds intValue())

    Note that autoboxing/autounboxing can introduce a significant overhead (especially inside loops).

    0 讨论(0)
  • 2021-02-02 10:16

    To figure out if an Integer is greater than 0, you can:

    • check if compareTo(O) returns a positive number:

      if (count.compareTo(0) > 0)
           ...
      

      But that looks pretty silly, doesn't it? Better just...

    • use autoboxing1:

      if (count > 0)
          ....
      

      This is equivalent to:

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

      It is important to note that "==" is evaluated like this, with the Integer operand unboxed rather than the int operand boxed. Otherwise, count == 0 would return false when count was initialized as new Integer(0) (because "==" tests for reference equality).

    1Technically, the first example uses autoboxing (before Java 1.5 you couldn't pass an int to compareTo) and the second example uses unboxing. The combined feature is often simply called "autoboxing" for short, which is often then extended into calling both types of conversions "autoboxing". I apologize for my lax usage of terminology.

    0 讨论(0)
  • 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) { }
    
    0 讨论(0)
提交回复
热议问题