Java Long Compare Method

JavaJavaBeginner
Practice Now

Introduction

The Long class in Java provides a compare() method that helps in comparing two long values for numerical difference. In this lab, we will explore the syntax, parameters, and return types of this method, and write down the implementation of several examples for better understanding.

Write the Initial Code

We will start by importing the java.lang.Long package, which holds the compare() method. We will set up a public class named LongCompare, which will hold the main() method. In the main() method, we will initialize the long values n1, n2, and n3 with values 15, 25, and 15 respectively. The method will then compare n1 and n2 with the compare() method and print out the result.

import java.lang.Long;

public class LongCompare {
    public static void main(String[] args){
        long n1 = 15L;
        long n2 = 25L;
        long n3 = 15L;

        System.out.print("Result of comparing n1 and n2: ");
        System.out.println(Long.compare(n1,n2));
    }
}

To run this code, we need to use the following command in the terminal:

javac LongCompare.java && java LongCompare

The output will look like this:

Result of comparing n1 and n2: -1

Compare Two Long Values

We will now implement the compare() method to compare two long values. We will set up long values largeNumber and smallNumber, and use an if-else statement to compare them. We will print output messages for different output scenarios.

import java.lang.Long;

public class LongCompare {
    public static void main(String args[]){
        long largeNumber = 100L;
        long smallNumber = 10L;

        if (Long.compare(largeNumber, smallNumber) > 0){
            System.out.println("First number is greater");
        } else if (Long.compare(largeNumber, smallNumber) < 0){
            System.out.println("Second number is greater");
        } else {
            System.out.println("Both numbers are equal");
        }
    }
}

To run this code, we need to use the following command in the terminal:

javac LongCompare.java && java LongCompare

The output will look like this:

First number is greater

Compare Two Long Arrays

We will now implement the compare() method to compare two long arrays. We will create two arrays named array1 and array2 with respective values, then we will use a for loop to iterate through both arrays and compare respective elements at the same index. Then we will create separate output messages for results greater, smaller, or equal to 0.

import java.lang.Long;

public class LongCompare {
    public static void main(String[] args){
        long[] array1 = {1L, 2L, 3L, 4L, 5L};
        long[] array2 = {2L, 2L, 3L, 4L, 7L};

        for (int i = 0; i < array1.length; i++) {
            int result = Long.compare(array1[i], array2[i]);

            if (result > 0){
                System.out.println(array1[i] + " is greater than " + array2[i]);
            } else if (result < 0){
                System.out.println(array2[i] + " is greater than " + array1[i]);
            } else {
                System.out.println(array1[i] + " and " + array2[i] + " are equal.");
            }
        }
    }
}

To run this code, we need to use the following command in the terminal:

javac LongCompare.java && java LongCompare

The output will look like this:

1 is less than 2
2 and 2 are equal.
3 and 3 are equal.
4 and 4 are equal.
5 is less than 7

User Interactive Code

We will now create an interactive Java program where the user can enter two long values and see the results of the comparison. We will use a Scanner to get input from the user and an if-else statement to compare the values. We will create separate output messages according to the different results.

import java.lang.Long;
import java.util.Scanner;

public class LongCompare {
    public static void main(String [] args) {
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter first number: ");
        long num1 = sc.nextLong();

        System.out.print("Enter second number: ");
        long num2 = sc.nextLong();

        if (Long.compare(num1, num2) > 0){
            System.out.println(num1 + " is greater than " + num2);
        } else if (Long.compare(num1, num2) < 0){
            System.out.println(num2 + " is greater than " + num1);
        } else {
            System.out.println("Both numbers are equal.");
        }
    }
}

To run this code, we need to use the following command in the terminal:

javac LongCompare.java && java LongCompare

The output will look like this:

Enter first number: 2350253
Enter second number: 62203982
62203982 is greater than 2350253

Summary

In this lab, we learned to import and use the Java Long compare() method to compare two long values or long arrays. We also implemented several examples to understand the compare() method better.

Other Java Tutorials you may like