VOOZH about

URL: https://www.geeksforgeeks.org/java/short-compare-method-in-java/

⇱ Short compare() method in Java - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Short compare() method in Java

Last Updated : 11 Jul, 2025
The compare() method of Short class is used to compare two primitive short values for numerical equality. As it is a static method therefore it can be used without creating any object of Short. Syntax:
public static int compare(short x, short y)
Parameters: This method accepts two parameters:
  • x: which is the first Short object to be compared.
  • y: which is the Short object to be compared.
Return type : It returns an int value. It returns:
  • 0 if 'x' is equal to 'y',
  • a positive value 'x' is greater than 'y',
  • a negative value 'x' is lesser than 'y'
Below is the implementation of compare() 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