Java Integer compare() method Last Updated : 05 Dec, 2018 Comments Improve Suggest changes Like Article Like Report 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 compare(int x, int y) Parameter : x : the first int to compare y : the second int to compare Return : This method 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. Example :To show working of java.lang.Integer.compare() method. java // Java program to demonstrate working // of java.lang.Integer.compare() method import java.lang.Integer; class Gfg { // driver code public static void main(String args[]) { int a = 10; int b = 20; // as 10 less than 20, Output will be a value less than zero System.out.println(Integer.compare(a, b)); int x = 30; int y = 30; // as 30 equals 30, Output will be zero System.out.println(Integer.compare(x, y)); int w = 15; int z = 8; // as 15 is greater than 8, Output will be a value greater than zero System.out.println(Integer.compare(w, z)); } } Output: -1 0 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 compareTo() method 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 Intege 2 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 How compare() method works in Java Prerequisite: Comparator Interface in Java, TreeSet in JavaThe compare() method in Java compares two class specific objects (x, y) given as parameters. It returns the value:Â 0: if (x==y)-1: if (x < y)1: if (x > y)Syntax:Â Â public int compare(Object obj1, Object obj2)where obj1 and obj2 are th 3 min read JavaTuples compareTo() method The compareTo() method in org.javatuples is used to compare the order of a Tuple object with the object passed as the parameter. This method can be used for any tuple class object of javatuples library. It returns the difference in the ASCII value of the 1st different element present in the passed o 3 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 Java String compareToIgnoreCase() Method In Java, the String compareToIgnoreCase() method compares two Strings lexicographically means in dictionary order by ignoring the case differences. It evaluates the Unicode values of characters sequentially.Example 1: Here, we will use the compareToIgnoreCase() method to compare strings that are lex 2 min read BigInteger compareTo() Method in Java The java.math.BigInteger.compareTo(BigInteger value) method Compares this BigInteger with the BigInteger passed as the parameter. Syntax: public int compareTo(BigInteger val)Parameter: This method accepts a single mandatory parameter val which is the BigInteger to compare with BigInteger object. Ret 2 min read Java Comparable Interface The Comparable interface in Java is used to define the natural ordering of objects for a user-defined class. It is part of the java.lang package and it provides a compareTo() method to compare instances of the class. A class has to implement a Comparable interface to define its natural ordering.Exam 4 min read Like