Java Integer compareTo() methodThe 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:
Exceptions:NA Compatibility Version:Java 1.2 and above Example 1Test it NowOutput: -1 0 1 Example 2Test it NowOutput: x is greater than y Example 3Test it NowOutput: 1 0 -1 Example 4Output: Enter the integer value to be campare with another Integer: 200 -1 0 1
Next TopiccompareUnsigned() Method
|