Short compare() method in Java Last Updated : 11 Jul, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report 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: Java // Java code to demonstrate // Short compare() method class GFG { public static void main(String[] args) { short a = 4; short b = 4; // compare method in Short class int output = Short.compare(a, b); // printing the output System.out.println("Comparing " + a + " and " + b + " : " + output); } } Output: Comparing 4 and 4 : 0 Example 2: Java // Java code to demonstrate // Short compare() method class GFG { public static void main(String[] args) { short a = 4; short b = 2; // compare method in Short class int output = Short.compare(a, b); // printing the output System.out.println("Comparing " + a + " and " + b + " : " + output); } } Output: Comparing 4 and 2 : 2 Example 3: Java // Java code to demonstrate // Short compare() method class GFG { public static void main(String[] args) { short a = 2; short b = 4; // compare method in Short class int output = Short.compare(a, b); // printing the output System.out.println("Comparing " + a + " and " + b + " : " + output); } } Output: Comparing 2 and 4 : -2 Comment More infoAdvertise with us Next Article Short compareTo() method in Java with Examples S Sruti Rai Follow Improve Article Tags : Misc Java Java - util package java-basics Java-lang package Java-Functions Java-Short +3 More Practice Tags : JavaMisc Similar Reads 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. 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 return 2 min read 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. 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 return 2 min read Stack empty() Method in Java The java.util.Stack.empty() method in Java is used to check whether a stack is empty or not. The method is of boolean type and returns true if the stack is empty else false. Syntax: STACK.empty() Parameters: The method does not take any parameters. Return Value: The method returns boolean true if th 4 min read Short equals() method in Java with Examples The equals() method of Short class is a built in method in Java which is used to compare the equality given Object with the instance of Short invoking the equals() method. Syntax ShortObject.equals(Object a) Parameters: It takes an Object type object a as input which is to be compared with the insta 2 min read Short equals() method in Java with Examples The equals() method of Short class is a built in method in Java which is used to compare the equality given Object with the instance of Short invoking the equals() method. Syntax ShortObject.equals(Object a) Parameters: It takes an Object type object a as input which is to be compared with the insta 2 min read Short hashCode() method in Java with Examples The hashCode() method of Short class is a built in method in Java which is used to return the hash code of the ShortObject. Note: The hashCode() returns the same value as intValue(). Syntax ShortObject.hashCode() Return Value: It return an int value which represents the hashcode of the specified Sho 2 min read Like