Java Guava | compare() method of Short Class with Examples
Shorts.compare() method of Guava’s Shorts Class is used to compare the two specified short values. These values are passed as the parameter and the result of comparison is found as the difference of 1st value and the 2nd value. Hence it can be positive, zero or negative.
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 code to show implementation of // Guava's Shorts.compare() method import com.google.common.primitives.Shorts; class GFG { public static void main(String[] args) { short a = 4; short b = 4; // compare method in Short class int output = Shorts.compare(a, b); // printing the output System.out.println("Comparing " + a + " and " + b + " : " + output); } } |
Comparing 4 and 4 : 0
Example 2:
// Java code to show implementation of // Guava's Shorts.compare() method import com.google.common.primitives.Shorts; class GFG { public static void main(String[] args) { short a = 4; short b = 2; // compare method in Short class int output = Shorts.compare(a, b); // printing the output System.out.println("Comparing " + a + " and " + b + " : " + output); } } |
Comparing 4 and 2 : 2
Example 3:
// Java code to show implementation of // Guava's Shorts.compare() method import com.google.common.primitives.Shorts; class GFG { public static void main(String[] args) { short a = 2; short b = 4; // compare method in Short class int output = Shorts.compare(a, b); // printing the output System.out.println("Comparing " + a + " and " + b + " : " + output); } } |
Comparing 2 and 4 : -2
Recommended Posts:
- Java Guava | Shorts.indexOf(short[] array, short target) method with Examples
- Java Guava | Shorts.indexOf(short[] array, short[] target) method with Examples
- Java Guava | Doubles.compare() method with Examples
- Java Guava | Booleans.compare() method with Examples
- Java Guava | Chars.compare() method with Examples
- Java Guava | Ints.compare() method with Examples
- Java Guava | Longs.compare() method with Examples
- Java Guava | Floats.compare() method with Examples
- Short compare() method in Java
- Java Guava | factorial(int n) method of IntMath Class with Examples
- Java Guava | binomial(int n, int k) of LongMath Class with Examples
- Java Guava | pow(long b, int k) of LongMath Class with Examples
- Java Guava | isPowerOfTwo(long x) of LongMath Class with Examples
- Java Guava | mod() method of IntMath Class
- Java Guava | mean() method of IntMath Class
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



