Compare two integer values

int compareTo(Integer anotherInteger)
Compares two Integer objects numerically.
boolean equals(Object obj)
Compares this object to the specified object.

Returns from compareTo(Integer anotherInteger)

ValueDescription
0if this Integer is equal to the argument Integer;
less than 0if this Integer is less than the argument Integer;
greater than 0if this Integer is greater than the argument Integer.

public class Main {
  public static void main(String[] args) {

    Integer integer1 = new Integer("1");
    Integer integer2 = new Integer("2");
    
    System.out.println(integer1.compareTo(integer2));

  }
}

The output:


-1
Home 
  Java Book 
    Essential Classes  

Integer:
  1. Integer class
  2. Max, min value of an integer type variable
  3. Create Integer using its constructors
  4. Return an integer value as byte, double, float, int, long and short
  5. Compare two integer values
  6. Decode a string and return an integer value
  7. Convert string to integer
  8. Convert integer to string
  9. Reverse and rotate the integer in binary format
  10. Bit oriented operation
  11. Return system property as integer
  12. Get the leading and trailing zeros
  13. Get the sign of an Integer
  14. Convert integer to binary, hexdecimal and octal format