Java Integer CompareUnsigned Method

JavaJavaBeginner
Practice Now

Introduction

The compareUnsigned() method of the Integer class is used to compare the unsigned value of two integers to find out which one is greater than the other. The method returns 0 if the unsigned values of both integers are equal. It returns 1 if the unsigned value of the first integer is greater than the second. It returns -1 if the unsigned value of the first integer is less than the second.


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/exceptions("`Exceptions`") 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/comments("`Comments`") java/BasicSyntaxGroup -.-> java/data_types("`Data Types`") 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-117700{{"`Java Integer CompareUnsigned Method`"}} java/classes_objects -.-> lab-117700{{"`Java Integer CompareUnsigned Method`"}} java/class_methods -.-> lab-117700{{"`Java Integer CompareUnsigned Method`"}} java/exceptions -.-> lab-117700{{"`Java Integer CompareUnsigned Method`"}} java/modifiers -.-> lab-117700{{"`Java Integer CompareUnsigned Method`"}} java/oop -.-> lab-117700{{"`Java Integer CompareUnsigned Method`"}} java/packages_api -.-> lab-117700{{"`Java Integer CompareUnsigned Method`"}} java/user_input -.-> lab-117700{{"`Java Integer CompareUnsigned Method`"}} java/wrapper_classes -.-> lab-117700{{"`Java Integer CompareUnsigned Method`"}} java/identifier -.-> lab-117700{{"`Java Integer CompareUnsigned Method`"}} java/arrays -.-> lab-117700{{"`Java Integer CompareUnsigned Method`"}} java/comments -.-> lab-117700{{"`Java Integer CompareUnsigned Method`"}} java/data_types -.-> lab-117700{{"`Java Integer CompareUnsigned Method`"}} java/if_else -.-> lab-117700{{"`Java Integer CompareUnsigned Method`"}} java/operators -.-> lab-117700{{"`Java Integer CompareUnsigned Method`"}} java/output -.-> lab-117700{{"`Java Integer CompareUnsigned Method`"}} java/strings -.-> lab-117700{{"`Java Integer CompareUnsigned Method`"}} java/variables -.-> lab-117700{{"`Java Integer CompareUnsigned Method`"}} java/system_methods -.-> lab-117700{{"`Java Integer CompareUnsigned Method`"}} end

Create a Java class file

In the terminal, create a new directory for your Java project. For example:

mkdir project

Move into the project directory:

cd project

Create a new Java class file named IntegerCompareUnsigned.java:

touch IntegerCompareUnsigned.java

Implement the code

Copy and paste the following code into the IntegerCompareUnsigned.java file:

import java.lang.Integer;
import java.util.Scanner;

public class IntegerCompareUnsigned {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        System.out.print("Enter first and second number: ");

	// Taking input from user
        try {
            int n1 = sc.nextInt();
            int n2 = sc.nextInt();

	    // Calling compareUnsigned() method
            int r = Integer.compareUnsigned(n1, n2);

	    // Examining the result
            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("Invalid input!");
        }
    }
}

Save and exit the file.

This program takes two integers from the user and uses the compareUnsigned() method to compare their unsigned values. It then displays whether the first number is greater than the second, the second number is greater than the first, or the numbers are equal.

Compile and run the program

Compile the program using the following command:

javac IntegerCompareUnsigned.java

Run the program using the following command:

java IntegerCompareUnsigned

You should see the following prompt:

Enter first and second number:

Enter two numbers - one greater than 2147483647 and the other less than or equal to 2147483647. This is because the Java int datatype can hold only signed 32-bit values - from -2147483648 to 2147483647. If you enter a number greater than 2147483647, an error message will be displayed.

The program will run, and you should see the following output:

First number is greater

Test the program with different values

You can test the program with different values and see the output.

Try running the program with the following inputs:

Enter first and second number: -10 20
Enter first and second number: 500000000 -200000000
Enter first and second number: 0 0

Modify the program

Modify the program to take two integers as command-line arguments instead of reading them from the user at run time.

Copy and paste the following code into the IntegerCompareUnsigned.java file:

import java.lang.Integer;

public class IntegerCompareUnsigned {

    public static void main(String[] args) {

	// Checking if the correct number of arguments are provided
        if(args.length != 2) {
            System.out.println("Please provide two integer arguments!");
            return;
        }

	// Parsing the arguments as integers
        int n1 = Integer.parseInt(args[0]);
        int n2 = Integer.parseInt(args[1]);

	// Calling compareUnsigned() method
        int r = Integer.compareUnsigned(n1, n2);

	// Examining the result
        if(r > 0) {
            System.out.println(n1 + " is greater than " + n2);
        }
        else if(r < 0) {
            System.out.println(n2 + " is greater than " + n1);
        }
        else {
            System.out.println(n1 + " and " + n2 + " are equal");
        }
    }
}

Save and exit the file.

Compile and run the modified program

Compile the modified program using the following command:

javac IntegerCompareUnsigned.java

Run the modified program using the following command:

java IntegerCompareUnsigned 3000000000 200000000

You should see the following output:

3000000000 is greater than 200000000

Modify the program to test it with different inputs.

Summary

In this lab, you learned how to use the compareUnsigned() method of the Integer class to compare the unsigned value of two integers to find out which one is greater than the other. You also learned how to modify the program to take two integers as command-line arguments instead of reading them from the user at run time.

Other Java Tutorials you may like