The Wayback Machine - https://web.archive.org/web/20240819040451/https://www.geeksforgeeks.org/java-integer-compareto-method/
Open In App

Java Integer compareTo() method

Last Updated : 03 Apr, 2023
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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


Similar Reads

Double.compareTo() Method in Java with Examples
The java.lang.Double.compareTo() is a built-in method in java that compares two Double objects numerically. This method returns 0 if this object is equal to the argument object, it returns less than 0 if this object is numerically less than the argument object and a value greater than 0 if this object is numerically greater than the argument object
3 min read
ByteBuffer compareTo() method in Java With Examples
The compareTo() method of java.nio.ByteBuffer class is used to compare one buffer to another. Two byte 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 byte elements are compared as if by invoking Byte.compare
4 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. Return Type: This method returns the following: 0: if
2 min read
FloatBuffer compareTo() method in Java With Examples
The compareTo() method of java.nio.FloatBuffer class is used to compare one buffer to another. Two float 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 float elements are compared as if by invoking Float.com
4 min read
CharBuffer compareTo() method in Java
compareTo() method of java.nio.charBuffer class is used to compare one buffer to another. Two char buffers are compared by comparing their sequences of remaining elements lexicographically, without considering the starting position of each sequence within its corresponding buffer. Pairs of char elements are compared as if by invoking Char.compare(c
4 min read
ShortBuffer compareTo method in Java with Examples
The compareTo() method of java.nio.ShortBuffer class is used to compare one buffer to another. Two short 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 short elements are compared as if by invoking Short.com
4 min read
DoubleBuffer compareTo() method in Java With Examples
The compareTo() method of java.nio.DoubleBuffer class is used to compare one buffer to another. Two double 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 float elements are compared as if by invoking Double.
3 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 elements are compared as if by invoking Int.compare(int
4 min read
Boolean compareTo() method in Java with examples
The compareTo() method of Boolean class is a built in method in Java which is used to compare the given Boolean instance with the current instance. Syntax: BooleanObject.compareTo(Boolean a) Parameters: It takes a Boolean value a as parameter which is to be compared with the current instance. Return Type: The return type of the function is int. It
2 min read
Byte compareTo() method in Java with examples
The compareTo() method of Byte class is a built in method in Java which is used to compare the given Byte type object with the instance of Byte invoking the compareTo() method. Syntax ByteObject.compareTo(Byte a) Parameters: It takes a Byte type object a as input which is to be compared with the instance of the Byte object calling the compareTo met
2 min read
Short compareTo() method in Java with Examples
The compareTo() method of Short class is a built in method in Java which is used to compare twoShort objects numerically. Syntax: public int compareTo(Short otherShort) Parameters : This method accepts a mandatory parameter otherShort which is the Short object to be compared. Return type : It returns an int value. It returns: 0 if 'otherShort' is e
2 min read
Float compareTo() method in Java with examples
The comapreTo() method of Float Class is a built-in method in Java that compares the two specified float values. The sign of the integer value returned is the same as that of the integer that would be returned by the function call. Syntax: public int compareTo(Object f) Parameters: The function accepts a mandatory parameter object f which is the va
2 min read
Instant compareTo() method in Java with Examples
The compareTo(Instant otherInstant) method of Instant class used to compare this instance to the instance passed as parameter to check whether both instances are equal. The comparison between both instances is based on the time-line position of the instants. The value to be returned by this method is determined as follows: if this instance is great
3 min read
Duration compareTo(Duration) method in Java with Examples
The compareTo(Duration) method of Duration Class in java.time package is used to compare this duration with the duration passed as the parameter. Syntax: public int compareTo(Duration otherDuration) Parameters: This method accepts a parameter otherDuration which is the duration to which this duration will be compared to. Return Value: This method r
2 min read
Year compareTo() method in Java with Examples
The compareTo() method of Year class in Java is used to compare this Year object with another Year object. The comparison of Year object is based on the values of Year.Syntax: public int compareTo(Year otherYear) Parameter: This method accepts a single parameter otherYear. It is the Year object which specifies a year with which we want to compare t
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 comparator value which is negative if it is less
1 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 int compareTo(YearMonth otherYearMonth) Parameter
2 min read
LocalDateTime compareTo() method in Java with Examples
The compareTo() method of LocalDateTime class in Java is used to compare this date-time to the date-time passed as the parameter. Syntax: public int compareTo(ChronoLocalDateTime anotherDate) Parameter: This method accepts a parameter anotherDate which specifies the other date-time to be compare to. It should not be null. Returns: The function retu
2 min read
LocalTime compareTo() method in Java with Examples
The compareTo() method of a LocalTime class is used to compare this LocalTime object to the LocalTime passed as parameter to check whether both LocalTimes are equal. The comparison between both LocalTimes is based on timeline position of the local times within a day. The value to be returned by this method is determined as follows: if this LocalTim
3 min read
MonthDay compareTo() method in Java with Examples
The compareTo() method of MonthDay class in Java compares this month-day to another month-day. Syntax: public int compareTo(MonthDay other) Parameter: This method accepts a parameter other which specifies the other month-day to compare to and not null. Returns: The function returns the comparator value. It can be negative if it is less or positive
2 min read
OffsetDateTime compareTo() method in Java with examples
The compareTo() method of OffsetDateTime class in Java compares this date-time to another date-time.Syntax : public int compareTo(OffsetDateTime other) Parameter : This method accepts a single parameter other which specifies the other date-time to compare to, not null.Return Value: It returns the comparator value, negative if less, positive if grea
2 min read
ZoneOffset compareTo(ZoneOffset) method in Java with Examples
The compareTo(ZoneOffset) method of ZoneOffset Class in java.time package is used to compare another instance of ZoneOffset passed as the parameter to this ZoneOffset instance. This method returns an integer value which is the comparator value. Syntax: public int compareTo(ZoneOffset anotherZoneOffset) Parameters: This method accepts accepts a para
2 min read
OffsetTime compareTo() method in Java with examples
The compareTo() method of OffsetTime class in Java compares this time to another time and returns zero if they are equal or a positive or negative integer depending on the comparison result. Syntax: public int compareTo(OffsetTime other) Parameter: This method accepts a single mandatory parameter other which specifies the other time which will be c
2 min read
UUID compareTo() Method in Java with Examples
The compareTo() method of UUID class in Java is used to compare one UUID value with another specified UUID. It returns -1 if this UUID is less than the value, 0 if this UUID is equal to the value, and 1 if this UUID is greater than the value. Syntax: UUID_1.compareTo(UUID_2) Parameters: The method takes one parameter UUID_2 to which the UUID_1 is t
2 min read
Date compareTo() method in Java with examples
The compareTo() method of Java Date class compares two dates and sort them for order. Syntax: public int compareTo(Date anotherDate) Parameters: The function accepts a single parameter anotherDate which specifies the date to be compared . Return Value: The function gives three return values specified below: It returns the value 0 if the argument Da
2 min read
Calendar compareTo() Method in Java with Examples
The add(Calendar Calendar2) method of Calendar class is used to compare the time values or the millisecond offsets of this Calendar object with the passed Calendar object. Syntax: public int compareTo(Calendar Calendar2) Parameters: The method takes one parameter Calendar2 of Calendar object type and refers to the object to be compared to this Cale
2 min read
Charset compareTo() method in Java with Examples
The compareTo() method is a built-in method of the java.nio.charset compares two charsets with each other. A comparison is done by their canonical names, without regard to case. Syntax: public final int compareTo?(Charset second) Parameters: The function accepts a single mandatory parameter second which specifies the charset to be compared with. Re
1 min read
ChronoLocalDate compareTo() method in Java with Examples
The compareTo() method of ChronoLocalDate interface 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 comparator value which is negative if i
1 min read
ChronoZonedDateTime compareTo() method in Java with Examples
The compareTo() method of ChronoZonedDateTime interface in Java method compares this date to another date. Syntax: default int compareTo(ChronoZonedDateTime 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 comparator value which is nega
2 min read
ChronoLocalDateTime compareTo() method in Java with Examples
The compareTo() method of ChronoLocalDateTime interface in Java method compares this date to another date. Syntax: default int compareTo(ChronoLocalDateTime 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 comparator value which is nega
1 min read
Practice Tags :
three90RightbarBannerImg