The compareTo()  method of Integer class compares two Integer objects numerically.


public static int compareTo(int anotherInteger)


The parameter ‘anotherInteger’ represents the Integer to be compared.

 Specified by

This method is specified by compareTo in interface Comparable< Integer >

 Return Value

This method returns an integer value.

  • It returns zero, if this Integer is equal to the parameter.
  • It returns positive one, if this Integer is numerically greater than the argument Integer.
  • It returns negative one, if this Integer is numerically smaller than the argument Integer.

Example 1


Example 2


Example 3


Pin It on Pinterest

Share This