Signup/Sign In

Java Long compareTo() Method

Java compareTo() method belongs to the Long class. This method is similar to the compare() method of the same class but the only difference is that here the Long Objects are compared numerically rather than the long values.

The compareTo(long object2) method returns the value

  • 0; if object1 is equal to object2

  • -1; if object1 is less than object2

  • 1; if object1 is greater than object2

Syntax:

public int compareTo(Long anotherLong)

Parameter:

The parameter includes the Long object to be compared.

Returns:

0,-1,1 depending upon the value of the argument long value.

Example 1:

Here, four Long objects n1, n2, n3, n4 are taken with respective positive values and are checked for the values are either less, more, or equal than the compared values.

import java.lang.Long;

public class StudyTonight 
{  
    public static void main(String[] args) 
    {          
        Long ob1 = 100L;  
        Long ob2 = 200L;  
        Long ob3 = 300L;  
        Long ob4 = 100L;  
          
        System.out.println(ob1.compareTo(ob2));  //Output will be less than zero
        
        System.out.println(ob3.compareTo(ob1)); // Output will be greater than zero  
        
        System.out.println(ob1.compareTo(ob4));  // Output will be equal to zero
    }  
}  


-1
1
0

Example 2:

Here, an array of Long objects are created and every object in the array is compared with the first object of the array.

import java.lang.Long;

public class StudyTonight 
{  
    public static void main(String[] args) 
    {          
        Long [] ob = {-100L,-200L,300L,-100L}; 
        for(int i=0;i<4;i++)
        {
            System.out.println(ob[0].compareTo(ob[i]));  // comparing each object of array with first object
        }
    }  
}


0
1
-1
0

Example 3:

Here is a general example where the user can enter the numbers of his choice and get the desired output. The try and catch block (Java Try and Catch) is used for handling any exception taking place during the execution of the program. (For example, the user enters a String, etc. in place of the long).

import java.util.Scanner; 
import java.lang.Long;
public class StudyTonight 
{  
    public static void main(String[] args) 
    {      
        Scanner sc = new Scanner(System.in);  
        System.out.print("Enter first and second number ");  
        try
        {
            Long n1 = sc.nextLong();  
            Long n2 = sc.nextLong();  
            int r =  n1.compareTo(n2);    
            if(r > 0)
            {  
                System.out.println("first number is greater");  
            }
            else if(r< 0) 
            {  
                System.out.println("second number is greater");  
            } 
            else
            {  
                System.out.println("both numbers are equal");
            }
        }
        catch(Exception e)
        {
            System.out.println("Error!!");
        }
    }  
}  


Enter first and second number 453 576
second number is greater
**********************************************
Enter first and second number 555 123
first number is greater
**********************************************
Enter first and second number 0x66 0x234
Error!!

Live Example:

Here, you can test the live code example. You can execute the example for different values, even can edit and write your examples to test the Java code.



About the author:
A Computer Science and Engineering Graduate(2016-2020) from JSSATE Noida. JAVA is Love. Sincerely Followed Sachin Tendulkar as a child, M S Dhoni as a teenager, and Virat Kohli as an adult.