Compare two unsigned numbers in Java

 Write a program that compares the given two numbers as unsigned.

Java program that compares the given two numbers as unsigned

public class Main {
   
    public static void main(String[] args) {
        
        int resultSigned = Integer.compare(Integer.MIN_VALUE, Integer.MAX_VALUE);
        int resultUnsigned = Integer.compareUnsigned(Integer.MIN_VALUE, Integer.MAX_VALUE);
        
        System.out.println("Signed ints: " + Integer.MIN_VALUE + ", " + Integer.MAX_VALUE);
        System.out.println("-----------------------------------------------");        
        System.out.println("Result of comparing signed: " + resultSigned);        
        System.out.println("Result of comparing unsigned: " + resultUnsigned);        
    } 
}

Output:

Signed ints: -2147483648, 2147483647
-----------------------------------------------
Result of comparing signed: -1
Result of comparing unsigned: 1

Comments