Java long compareTo() with examples
The java.lang.Long.compareTo() is a built-in method in java that compares two Long objects numerically. This method returns 0 if this object is equal to the argument object, it returns less than 0 if this object is numerically less than the argument object and a value greater than 0 if this object is numerically greater than the argument object.
Syntax:
public int compareTo(Object obj) Parameter: obj - The object which is to be compared to.
Returns: The function returns three values:
- equal to 0: Object is equal to the argument object
- less than 0: Object is less than the argument object
- greater than 0: Object is greater than the argument object
Program 1: The program below demonstrates the working of function.
// Java program to demonstrate // of java.lang.Long.compareTo() method import java.lang.Math; class Gfg1 { // driver code public static void main(String args[]) { // when two objects are different Long obj1 = new Long(124); Long obj2 = new Long(167); int compareValue = obj1.compareTo(obj2); if (compareValue == 0) System.out.println("object1 and object2 are equal"); else if (compareValue < 0) System.out.println("object1 is less than object2"); else System.out.println("object1 is greater than object2"); } } |
Output:
object1 is less than object2
Program 2: The program below demonstrates the working of function when no argument is passed
// Java program to demonstrate // of java.lang.Long.compareTo() method import java.lang.Math; class Gfg1 { // driver code public static void main(String args[]) { // when no argument is passed Long obj1 = new Long(124); Long obj2 = new Long(167); int compareValue = obj1.compareTo(); if (compareValue == 0) System.out.println("object1 and object2 are equal"); else if (compareValue < 0) System.out.println("object1 is less than object2"); else System.out.println("object1 is greater than object2"); } } |
Output:
prog.java:14: error: method compareTo in class Long cannot be applied to given types;
int compareValue = obj1.compareTo();
^
required: Long
found: no arguments
reason: actual and formal argument lists differ in length
1 error
Program 3: The program below demonstrates the working of function when anything other than object is passed in an argument
// Java program to demonstrate // of java.lang.Long.compareTo() method import java.lang.Math; class Gfg1 { // driver code public static void main(String args[]) { // when anything other than object // argument is passed Long obj1 = new Long(124); int compareValue = obj1.compareTo("gfg"); if (compareValue == 0) System.out.println("object1 and object2 are equal"); else if (compareValue < 0) System.out.println("object1 is less than object2"); else System.out.println("object1 is greater than object2"); } } |
Output:
prog.java:14: error: incompatible types: String cannot be converted to Long
int compareValue = obj1.compareTo("gfg");
^
Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output
1 error
Recommended Posts:
- Java Guava | LongMath.divide(long, long, RoundingMode) method with Examples
- Java Guava | Longs.checkedSubtract(long a, long b) method with Examples
- Java Guava | LongMath.checkedAdd(long a, long b) method with Examples
- Java Guava | Longs.indexOf(long[] array, long target) method with Examples
- Java Guava | Longs.indexOf(long[] array, long[] target) method with Examples
- Java Guava | mod(long x, long m) of LongMath Class with Examples
- Duration ofSeconds(long, long) method in Java with Examples
- Java Guava | mean(long x, long y) of LongMath Class with Examples
- Java Guava | gcd(long a, long b) of LongMath Class with Examples
- UUID compareTo() Method in Java with Examples
- OffsetTime compareTo() method in Java with examples
- Calendar compareTo() Method in Java with Examples
- Double.compareTo() Method in Java with Examples
- ChronoZonedDateTime compareTo() method in Java with Examples
- Byte compareTo() method in Java with examples
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.



