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.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL java(("`Java`")) -.-> java/ProgrammingTechniquesGroup(["`Programming Techniques`"]) java(("`Java`")) -.-> java/ObjectOrientedandAdvancedConceptsGroup(["`Object-Oriented and Advanced Concepts`"]) java(("`Java`")) -.-> java/BasicSyntaxGroup(["`Basic Syntax`"]) java(("`Java`")) -.-> java/DataStructuresGroup(["`Data Structures`"]) java(("`Java`")) -.-> java/StringManipulationGroup(["`String Manipulation`"]) java(("`Java`")) -.-> java/SystemandDataProcessingGroup(["`System and Data Processing`"]) java/ProgrammingTechniquesGroup -.-> java/scope("`Scope`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/classes_objects("`Classes/Objects`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/class_methods("`Class Methods`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/modifiers("`Modifiers`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/oop("`OOP`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/packages_api("`Packages / API`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/user_input("`User Input`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/wrapper_classes("`Wrapper Classes`") java/BasicSyntaxGroup -.-> java/identifier("`Identifier`") java/DataStructuresGroup -.-> java/arrays("`Arrays`") java/BasicSyntaxGroup -.-> java/data_types("`Data Types`") java/BasicSyntaxGroup -.-> java/for_loop("`For Loop`") java/BasicSyntaxGroup -.-> java/if_else("`If...Else`") java/BasicSyntaxGroup -.-> java/operators("`Operators`") java/BasicSyntaxGroup -.-> java/output("`Output`") java/StringManipulationGroup -.-> java/strings("`Strings`") java/BasicSyntaxGroup -.-> java/variables("`Variables`") java/SystemandDataProcessingGroup -.-> java/system_methods("`System Methods`") subgraph Lab Skills java/scope -.-> lab-117868{{"`Java Long Compare Method`"}} java/classes_objects -.-> lab-117868{{"`Java Long Compare Method`"}} java/class_methods -.-> lab-117868{{"`Java Long Compare Method`"}} java/modifiers -.-> lab-117868{{"`Java Long Compare Method`"}} java/oop -.-> lab-117868{{"`Java Long Compare Method`"}} java/packages_api -.-> lab-117868{{"`Java Long Compare Method`"}} java/user_input -.-> lab-117868{{"`Java Long Compare Method`"}} java/wrapper_classes -.-> lab-117868{{"`Java Long Compare Method`"}} java/identifier -.-> lab-117868{{"`Java Long Compare Method`"}} java/arrays -.-> lab-117868{{"`Java Long Compare Method`"}} java/data_types -.-> lab-117868{{"`Java Long Compare Method`"}} java/for_loop -.-> lab-117868{{"`Java Long Compare Method`"}} java/if_else -.-> lab-117868{{"`Java Long Compare Method`"}} java/operators -.-> lab-117868{{"`Java Long Compare Method`"}} java/output -.-> lab-117868{{"`Java Long Compare Method`"}} java/strings -.-> lab-117868{{"`Java Long Compare Method`"}} java/variables -.-> lab-117868{{"`Java Long Compare Method`"}} java/system_methods -.-> lab-117868{{"`Java Long Compare Method`"}} end

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