VOOZH about

URL: https://www.geeksforgeeks.org/java/java-guava-longs-compare-method-with-examples/

⇱ Java Guava | Longs.compare() method with Examples - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Java Guava | Longs.compare() method with Examples

Last Updated : 11 Jul, 2025
Longs.compare() method of Guava's Longs Class is used to compare the two specified long 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(long a, long b)
Parameters: This method accepts two parameters:
  • a: which is the first long object to be compared.
  • b: which is the second long object to be compared.
Return type: This method returns an int value. It returns:
  • 0 if ‘a’ is equal to ‘b’,
  • a positive value ‘a’ is greater than ‘b’,
  • a negative value ‘a’ is lesser than ‘b’
Exceptions: The method does not have any exception. Example 1:
Output:
Comparing 5 and 5 : 0
Example 2:
Output:
Comparing 5 and 4 : 1
Example 3:
Output:
Comparing 4 and 5 : -1
Comment
Article Tags: