Java Integer compareTo() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Integer

Definition

In Java, a.compareTo(b) compares the Integer object a with the Integer b. It returns a negative number if a is less, zero if both values are equal, and a positive number if a is greater. For Integer the result is exactly -1, 0, or 1.

This method makes Integer implement Comparable<Integer>, so lists and sorted collections know how to order numbers without any extra code.

An instance method of java.lang.Integer (since Java 1.2): call it on an Integer object. Integer lives in java.lang, so no import is needed.

Syntax

Call it on an Integer and pass another Integer or an int (Java boxes it).

Java
Integer a = 5;
int x = a.compareTo(9);   // -1: 5 is less than 9
int y = a.compareTo(5);   // 0
int z = a.compareTo(2);   // 1
CallWhat it does
a.compareTo(b)Compare a with b.
b.compareTo(a)The opposite direction gives the opposite sign.
Collections.sort(list)Uses compareTo to put Integer elements in natural order.
Integer.compare(x, y)The static version for primitive int values.

Test the sign of the result instead of comparing it to -1 or 1 when you write general code, because other classes return other numbers.

Library declarations
Java
// java.lang.Integer
public int compareTo(Integer anotherInteger)

// Required by Comparable<Integer>

Parameters

ParameterDescription
anotherInteger (Integer)The object to compare with. It must not be null.

Return Value

Return ValueDescription
-1This value is numerically less than the argument.
0Both values are equal.
1This value is numerically greater than the argument.

Compare with Similar Methods

Related comparisons:

MethodHow it differs
Integer.compare(x, y)Static, works on primitive int values without boxing.
equals(o)Only says whether the values are equal, not which is larger.
compareUnsigned(x, y)Orders the same bits as unsigned numbers.
String.compareTo()Orders text, and returns the difference between characters.

Java Examples

Example 1 — Compare Integer Objects

Compare an Integer with smaller, equal, and larger values.

Java
public class IntegerCompareToDemo {
    public static void main(String[] args) {
        Integer price = 50;

        System.out.println("50 vs 20:  " + price.compareTo(20));
        System.out.println("50 vs 50:  " + price.compareTo(50));
        System.out.println("50 vs 90:  " + price.compareTo(90));

        Integer other = 90;
        System.out.println("90 vs 50:  " + other.compareTo(price));
    }
}

How It Works

  1. An int is boxed. The argument 20 is turned into an Integer automatically before the comparison.
  2. Greater gives 1. 50 is larger than 20, so the result is 1.
  3. Equal gives 0. The same value on both sides returns 0.
  4. Smaller gives -1, and order matters. 50 compared with 90 is -1, while 90 compared with 50 is 1.

Example 2 — Sort and Search a List by Natural Order

Let Collections use compareTo to sort a list and find its smallest and largest element.

Java
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class IntegerCompareToSort {
    public static void main(String[] args) {
        List<Integer> temperatures = new ArrayList<>(Arrays.asList(21, -4, 15, 30, 0));

        Collections.sort(temperatures);
        System.out.println("sorted:  " + temperatures);
        System.out.println("lowest:  " + Collections.min(temperatures));
        System.out.println("highest: " + Collections.max(temperatures));
        System.out.println("0 vs -4: " + temperatures.get(1).compareTo(temperatures.get(0)));
    }
}

How It Works

  1. No comparator needed. Integer is Comparable, so Collections.sort orders the list using compareTo.
  2. Negative values sort first. The list begins with -4 and ends with 30.
  3. min and max. Both use the same ordering, so they return the first and last element of the sorted list.
  4. One direct call. The element at index 1 is 0 and the one at index 0 is -4, so 0.compareTo(-4) is 1.

Example 3 — See How It Differs from String.compareTo

Compare numbers and text with compareTo, and see what a null argument does.

Java
public class IntegerCompareToTraps {
    public static void main(String[] args) {
        Integer one = 1;
        System.out.println("1 vs 100:           " + one.compareTo(100));
        System.out.println("apple vs cherry:    " + "apple".compareTo("cherry"));
        System.out.println("apple vs apple pie: " + "apple".compareTo("apple pie"));

        try {
            one.compareTo(null);
        } catch (NullPointerException e) {
            System.out.println("compareTo(null) -> NullPointerException");
        }
    }
}

How It Works

  1. Always -1, 0, or 1. Comparing 1 with 100 does not give -99; Integer.compareTo only reports the direction.
  2. Strings report a difference. 'a' and 'c' are two letters apart, so "apple".compareTo("cherry") is -2.
  3. Strings report a length gap. When one text is the start of the other, the result is the difference in length, here -4.
  4. null is not allowed. There is nothing to compare with, so NullPointerException is thrown.

Frequently Asked Questions

It returns -1 if this Integer is less than the argument, 0 if they are equal, and 1 if it is greater.
Because subtraction can overflow. It reports only the direction, so the answer is always -1, 0, or 1.
No. compareTo(null) throws NullPointerException, so check for null first.
equals only tells you whether the values are the same. compareTo also tells you which one is larger, which is what sorting needs.
Use the static Integer.compare(x, y). A primitive int has no methods, so compareTo cannot be called on it.

Next: compareUnsigned()

Compare two ints as unsigned 32-bit numbers.

compareUnsigned() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

10 people found this page helpful