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.
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:
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.