Java Integer compareTo() methodLast Updated : 17 Mar 2025 The compareTo() method is a method of Integer class under java.lang package. This method compares two integer objects numerically. It returns the result of the value 0 if Integer is equal to the argument Integer, a value less than 0 if Integer is less than the argument Integer and a value greater than 0 if Integer is greater than the argument Integer. This method is specified by Comparable<Integer>Interface. Note: This method cannot be compared two different types of argument, so both the argument and the number should be the same type.SyntaxFollowing is the declaration of compare() method: Parameter:
Returns:This method will returns the following values: Exceptions:NA Compatibility Version:Java 1.2 and above Example 1Output: -1 0 1 Example 2Output: x is greater than y Example 3Output: 1 0 -1 Example 4Output: Enter the integer value to be campare with another Integer: 200 -1 0 1 Next TopicJava-integer-compareunsigned-method |
We request you to subscribe our newsletter for upcoming updates.

We deliver comprehensive tutorials, interview question-answers, MCQs, study materials on leading programming languages and web technologies like Data Science, MEAN/MERN full stack development, Python, Java, C++, C, HTML, React, Angular, PHP and much more to support your learning and career growth.
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India