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.
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:
Comparing 4 and 4 : 0
Comparing 4 and 2 : 2
Comparing 2 and 4 : -2
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.