Java Integer compareTo() method Last Updated : 03 Apr, 2023 Summarize Comments Improve Suggest changes Share Like Article Like Report The compareTo() method of Integer class of java.lang package compares two Integer objects numerically and returns the value 0 if this Integer is equal to the argument Integer; a value less than 0 if this Integer is numerically less than the argument Integer; and a value greater than 0 if this Integer is numerically greater than the argument Integer (signed comparison). Syntax : public int compareTo(Integer anotherInt) Parameter : anotherInt- : the Integer to be compared. Return : - This method returns the value 0 if this Integer is equal to the argument Integer; - a value less than 0 if this Integer is numerically less than the argument Integer; - and a value greater than 0 if this Integer is numerically greater than the argument Integer (signed comparison). Example 01 : To show working of java.lang.Integer.compareTo() method. java // Java program to demonstrate working // of java.lang.Integer.compareTo() method import java.lang.Integer; class Gfg { // driver code public static void main(String args[]) { Integer a = new Integer(10); Integer b = new Integer(20); // as 10 less than 20, Output will be a value less than zero System.out.println(a.compareTo(b)); Integer x = new Integer(30); Integer y = new Integer(30); // as 30 equals 30, Output will be zero System.out.println(x.compareTo(y)); Integer w = new Integer(15); Integer z = new Integer(8); // as 15 is greater than 8, Output will be a value greater than zero System.out.println(w.compareTo(z)); } } Output: -1 0 1 Example 02 :The working of compareTo() method in Java to compare two integer values. Java import java.io.*; class GFG { public static void main(String[] args) { Integer num1 = 10; Integer num2 = 5; Integer num3 = 10; int result1 = num1.compareTo(num2); int result2 = num1.compareTo(num3); int result3 = num2.compareTo(num3); System.out.println("Comparing num1 and num2: " + result1); System.out.println("Comparing num1 and num3: " + result2); System.out.println("Comparing num2 and num3: " + result3); } } Output : Comparing num1 and num2: 1 Comparing num1 and num3: 0 Comparing num2 and num3: -1 Comment More infoAdvertise with us Next Article Java Integer compare() method N Niraj_Pandey Follow Improve Article Tags : Java Java-lang package Java-Functions Java-Integer Practice Tags : Java Similar Reads Java Integer compare() method The compare() method of Integer class of java.lang package compares two integer values (x, y) given as a parameter and returns the value zero if (x==y), if (x < y) then it returns a value less than zero and if (x > y) then it returns a value greater than zero. Syntax : public static int compar 2 min read IntBuffer compareTo() method in Java The compareTo() method of java.nio.IntBuffer class is used to compare one buffer to another. Two int buffers are compared by comparing their sequences of remaining elements lexicographically, without regard to the starting position of each sequence within its corresponding buffer. Pairs of int eleme 4 min read Java Integer compareUnsigned() method The compareUnsigned() method of the Integer class of java.lang package compares two integer values (x, y) given as a parameter, treating the values as unsigned and returns the value zero if (x==y), if (x < y) then it returns a value less than zero, and if (x > y) then it returns a value greate 2 min read Short compare() method in Java The compare() method of Short class is used to compare two primitive short values for numerical equality. As it is a static method therefore it can be used without creating any object of Short. Syntax: public static int compare(short x, short y) Parameters: This method accepts two parameters: x: whi 2 min read YearMonth compareTo() method in Java The compareTo() method of YearMonth class in Java is used to compare two YearMonth objects. It compares this YearMonth object to the YearMonth object passed to it as parameter. The comparison between the two YearMonth instances is first done on the value of Year and then on the Month. Syntax: public 2 min read LocalDate compareTo() method in Java The compareTo() method of LocalDate class in Java method compares this date to another date. Syntax: public int compareTo(ChronoLocalDate other) Parameter: This method accepts a parameter other which specifies the other date to compare to and it is not specifically null. Return Value: It returns the 1 min read Like