Java Guava | Longs.compare() method with Examples Last Updated : 11 Jul, 2025 Comments Improve Suggest changes Like Article Like Report 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: Java // Java code to show implementation of // Guava's Longs.compare() method import com.google.common.primitives.Longs; class GFG { public static void main(String[] args) { long a = 5L; long b = 5L; // compare method in Long class int output = Longs.compare(a, b); // printing the output System.out.println("Comparing " + a + " and " + b + " : " + output); } } Output: Comparing 5 and 5 : 0 Example 2: Java // Java code to show implementation of // Guava's Longs.compare() method import com.google.common.primitives.Longs; class GFG { public static void main(String[] args) { long a = 5L; long b = 4L; // compare method in Long class int output = Longs.compare(a, b); // printing the output System.out.println("Comparing " + a + " and " + b + " : " + output); } } Output: Comparing 5 and 4 : 1 Example 3: Java // Java code to show implementation of // Guava's Longs.compare() method import com.google.common.primitives.Longs; class GFG { public static void main(String[] args) { long a = 4L; long b = 5L; // compare method in Long class int output = Longs.compare(a, b); // printing the output System.out.println("Comparing " + a + " and " + b + " : " + output); } } Output: Comparing 4 and 5 : -1 Comment More infoAdvertise with us Next Article Java Guava | Chars.compare() method with Examples S Sahil_Bansall Follow Improve Article Tags : Java Java-Functions java-guava Guava-Longs Practice Tags : Java Similar Reads Java Guava | Longs.contains() method with Examples The Longs.contains() method of Guava's Longs Class is used to check if a value i.e. target is present in the array or not. This target value and the array are taken as parameters to this method. And this method returns a boolean value that states whether the target value is Syntax: public static boo 2 min read Java Guava | Longs.contains() method with Examples The Longs.contains() method of Guava's Longs Class is used to check if a value i.e. target is present in the array or not. This target value and the array are taken as parameters to this method. And this method returns a boolean value that states whether the target value is Syntax: public static boo 2 min read Java Guava | Chars.compare() method with Examples Chars.compare() method of Guava's Chars Class is used to compare the two specified char 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 compa 2 min read Java Guava | Chars.compare() method with Examples Chars.compare() method of Guava's Chars Class is used to compare the two specified char 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 compa 2 min read Java Guava | Floats.compare() method with Examples Floats.compare() method of Floats Class is used to compare the two specified float 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(fl 2 min read Java Guava | Floats.compare() method with Examples Floats.compare() method of Floats Class is used to compare the two specified float 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(fl 2 min read Like