java.lang.Long compareTo(Long anotherLong)
Description
Notes:
- specified by compareTo in interface Comparable
Method Syntax
public int compareTo(Long anotherLong)
Method Argument
Data Type | Parameter | Description |
---|---|---|
Long | anotherLong | the Long to be compared. |
Method Returns
The compareTo(Long anotherLong) method of Longclass returns the value 0 if this Long is equal to the argument Long; a value less than 0 if this Long is numerically less than the argument Long; and a value greater than 0 if this Long is numerically greater than the argument Long (signed comparison).
Compatibility
Requires Java 1.2 and up
Java Long compareTo(Long anotherLong) Example
Below is a simple java example on the usage of compareTo(Long anotherLong) method of Long class.
package com.javatutorialhq.java.examples; import java.util.Scanner; /* * This example source code demonstrates the use of * compareTo(Long anotherLong) method of Long class */ public class LongCompareToExample { public static void main(String[] args) { // Ask for user input System.out.print("Enter the first value:"); // declare a scanner object to read the user input Scanner s = new Scanner(System.in); // assign the input to a variable Long value1 = s.nextLong(); // Ask for another value System.out.print("Enter the second value:"); Long value2 = s.nextLong(); // compare the two user input int result = value1.compareTo(value2); // interpret the result if(result == 0){ System.out.println("They are equal"); }else if(result > 0){ System.out.println("First Value is greater than the second"); }else{ System.out.println("First Value is less than the second"); } // close the scanner object s.close(); } }
Sample Output
Below is the sample output when you run the above example.