How to properly compare two Integers in Java?

前端 未结 10 1364
情歌与酒
情歌与酒 2020-11-21 07:06

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

Integer a = 4;
if (a < 5)

a will automaticall

相关标签:
10条回答
  • 2020-11-21 07:38

    Because comparaison method have to be done based on type int (x==y) or class Integer (x.equals(y)) with right operator

    public class Example {
    
        public static void main(String[] args) {
         int[] arr = {-32735, -32735, -32700, -32645, -32645, -32560, -32560};
    
            for(int j=1; j<arr.length-1; j++)
                if((arr[j-1]!=arr[j]) && (arr[j]!=arr[j+1])) 
                    System.out.println("int>"+arr[j]);
    
    
        Integer[] I_arr = {-32735, -32735, -32700, -32645, -32645, -32560, -32560};
    
            for(int j=1; j<I_arr.length-1; j++)
                if((!I_arr[j-1].equals(I_arr[j])) && (!I_arr[j].equals(I_arr[j+1]))) 
                    System.out.println("Interger>"+I_arr[j]);
        }
    }
    
    0 讨论(0)
  • 2020-11-21 07:41

    Since Java 1.7 you can use Objects.equals:

    java.util.Objects.equals(oneInteger, anotherInteger);
    

    Returns true if the arguments are equal to each other and false otherwise. Consequently, if both arguments are null, true is returned and if exactly one argument is null, false is returned. Otherwise, equality is determined by using the equals method of the first argument.

    0 讨论(0)
  • 2020-11-21 07:45

    Calling

    if (a == b)
    

    Will work most of the time, but it's not guaranteed to always work, so do not use it.

    The most proper way to compare two Integer classes for equality, assuming they are named 'a' and 'b' is to call:

    if(a != null && a.equals(b)) {
      System.out.println("They are equal");
    }
    

    You can also use this way which is slightly faster.

       if(a != null && b != null && (a.intValue() == b.intValue())) {
          System.out.println("They are equal");
        } 
    

    On my machine 99 billion operations took 47 seconds using the first method, and 46 seconds using the second method. You would need to be comparing billions of values to see any difference.

    Note that 'a' may be null since it's an Object. Comparing in this way will not cause a null pointer exception.

    For comparing greater and less than, use

    if (a != null && b!=null) {
        int compareValue = a.compareTo(b);
        if (compareValue > 0) {
            System.out.println("a is greater than b");
        } else if (compareValue < 0) {
            System.out.println("b is greater than a");
        } else {
                System.out.println("a and b are equal");
        }
    } else {
        System.out.println("a or b is null, cannot compare");
    }
    
    0 讨论(0)
  • 2020-11-21 07:46

    We should always go for equals() method for comparison for two integers.Its the recommended practice.

    If we compare two integers using == that would work for certain range of integer values (Integer from -128 to 127) due to JVM's internal optimisation.

    Please see examples:

    Case 1:

    Integer a = 100; Integer b = 100;

    if (a == b) {
        System.out.println("a and b are equal");
    } else {
       System.out.println("a and b are not equal");
    }
    

    In above case JVM uses value of a and b from cached pool and return the same object instance(therefore memory address) of integer object and we get both are equal.Its an optimisation JVM does for certain range values.

    Case 2: In this case, a and b are not equal because it does not come with the range from -128 to 127.

    Integer a = 220; Integer b = 220;

    if (a == b) {
        System.out.println("a and b are equal");
    } else {
       System.out.println("a and b are not equal");
    }
    

    Proper way:

    Integer a = 200;             
    Integer b = 200;  
    System.out.println("a == b? " + a.equals(b)); // true
    

    I hope this helps.

    0 讨论(0)
提交回复
热议问题