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

 Syntax

public static int compareTo(int anotherInteger)

 Parameters

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

Output

Example 2

Output

Example 3

Output

Pin It on Pinterest

Share This