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 equal to 'thisShort',
- a positive value 'thisShort' is lesser than 'otherShort',
- a negative value 'otherShort' is greater than 'thisShort'
Below is the implementation of compareTo() method in Java:
Example 1:
Output:
Comparing 4 and 4 : 0
Example 2:
Output:
Comparing 4 and 2 : 2
Example 3: