VOOZH about

URL: https://www.geeksforgeeks.org/java/short-compareto-method-in-java-with-examples/

⇱ Short compareTo() method in Java with Examples - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Short compareTo() method in Java with Examples

Last Updated : 11 Jul, 2025
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:
Output:
Comparing 2 and 4 : -2
Comment