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.
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.



