Java Arrays.compareUnsigned() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Java 9+

Definition

In Java, Arrays.compareUnsigned(a, b) works like Arrays.compare: it finds the first index where two arrays differ and orders them by those elements, with a shorter prefix coming first. The difference is that elements are compared as unsigned numbers.

Java's byte, short, int, and long are signed, so the byte 0xFF is -1. Read as unsigned, the same bits mean 255. Binary data such as hashes, network packets, and database keys is usually meant to be ordered that way, and that is where this method helps.

A static method of java.util.Arrays (Java 9+): call it on the class, as in Arrays.compareUnsigned(...); there is no Arrays object to create.

Java 9+ only.

Added in Java 9: the examples need JDK 9 or newer, and on Java 8 the call fails with "cannot find symbol". On Java 8, compare the elements in a loop with Byte.toUnsignedInt, Integer.compareUnsigned, or Long.compareUnsigned, all of which exist since Java 8.

Syntax

Pass two arrays of the same integer type; check the sign.

Java
byte[] a = {(byte) 0xFF};   // -1 signed, 255 unsigned
byte[] b = {1};
Arrays.compare(a, b) < 0;           // true:  -1 < 1
Arrays.compareUnsigned(a, b) > 0;   // true: 255 > 1
CallWhat it does
Arrays.compareUnsigned(byte[] a, byte[] b)Bytes as 0 to 255.
Arrays.compareUnsigned(int[] a, int[] b)Ints as 0 to 4294967295.
Arrays.compareUnsigned(a, aFrom, aTo, b, bFrom, bTo)Compares a range of each array.
Arrays.sort(keys, (x, y) -> Arrays.compareUnsigned(x, y))Sort binary keys in unsigned order.

Only the four integer types have it: byte[], short[], int[], and long[], each with a range form. For non-negative values the result agrees with compare; it differs only when a negative value is involved.

Library declarations
Java
// java.util.Arrays (since Java 9)
public static int compareUnsigned(byte[] a, byte[] b)
public static int compareUnsigned(byte[] a, int aFromIndex, int aToIndex,
                                  byte[] b, int bFromIndex, int bToIndex)
public static int compareUnsigned(int[] a, int[] b)
// also short[] and long[], each with a range form

Parameters

ParameterDescription
a, bThe arrays to compare. In the two-array forms, either may be null.
range indexes (optional)From (inclusive) and to (exclusive) indexes for each array.

Return Value

Return ValueDescription
intNegative, zero, or positive, like compare. Test only the sign.

Compare with Similar Methods

Signed and unsigned helpers:

MethodHow it differs
Arrays.compare(a, b)Signed comparison; -1 is less than 1.
Arrays.mismatch(a, b)Index of the first different element; sign does not matter.
Byte.toUnsignedInt(b)Converts one byte to its 0 to 255 value (Java 8).
Arrays.equals(a, b)Equality is the same signed or unsigned.

Java Examples

Example 1 — One Byte, Two Answers

The byte 0xFF compared with 1, first signed and then unsigned.

Java
import java.util.Arrays;

public class ArraysCompareUnsignedDemo {
    public static void main(String[] args) {
        byte[] a = {(byte) 0xFF};
        byte[] b = {1};
        System.out.println("a[0] as signed:   " + a[0]);
        System.out.println("a[0] as unsigned: " + Byte.toUnsignedInt(a[0]));
        System.out.println("compare:          " + Integer.signum(Arrays.compare(a, b)));
        System.out.println("compareUnsigned:  " + Integer.signum(Arrays.compareUnsigned(a, b)));
    }
}

How It Works

  1. Eight bits set. (byte) 0xFF stores the bit pattern 11111111, which Java's signed byte reads as -1.
  2. Same bits, other meaning. Byte.toUnsignedInt reads the pattern as 255.
  3. Signed order. Arrays.compare sees -1 versus 1, so a comes first and the sign is -1.
  4. Unsigned order. compareUnsigned sees 255 versus 1, so a comes last and the sign is 1.

Example 2 — Sort Byte Keys in Unsigned Order

Order four binary keys the way a byte-ordered store would, then print them in hex.

Java
import java.util.Arrays;

public class ArraysCompareUnsignedSortKeys {
    public static void main(String[] args) {
        byte[][] keys = { {(byte) 0x80}, {0x10, 0x01}, {0x7F}, {0x10} };
        Arrays.sort(keys, (x, y) -> Arrays.compareUnsigned(x, y));
        for (byte[] k : keys) {
            System.out.println(toHex(k));
        }
    }

    static String toHex(byte[] bytes) {
        StringBuilder sb = new StringBuilder();
        for (byte v : bytes) {
            sb.append(String.format("%02X ", Byte.toUnsignedInt(v)));
        }
        return sb.toString().trim();
    }
}

How It Works

  1. A high byte. 0x80 does not fit in a signed byte, so it needs a cast and is stored as -128.
  2. Prefix before longer key. {0x10} is a prefix of {0x10, 0x01}, so it sorts first.
  3. 0x80 goes last. As unsigned values, 0x7F is 127 and 0x80 is 128; a signed sort would have put 0x80 (-128) at the very top.
  4. Hex output. toHex converts each byte to 0 to 255 and formats it with %02X, two uppercase hex digits.

Example 3 — Unsigned int Comparison and a Range

A negative int becomes a huge unsigned value; then compare just the first element of each array.

Java
import java.util.Arrays;

public class ArraysCompareUnsignedInts {
    public static void main(String[] args) {
        int[] big = {7, -2};
        int[] small = {7, 5};
        System.out.println("-2 unsigned = " + Integer.toUnsignedString(-2));
        System.out.println("signed:   " + Integer.signum(Arrays.compare(big, small)));
        System.out.println("unsigned: " + Integer.signum(Arrays.compareUnsigned(big, small)));
        System.out.println("first elements only: " + Arrays.compareUnsigned(big, 0, 1, small, 0, 1));
    }
}

How It Works

  1. Two's complement. As an unsigned 32-bit number, -2 is 232 - 2, which Integer.toUnsignedString prints as 4294967294.
  2. The 7s tie. Both arrays start with 7, so index 1 decides: -2 versus 5.
  3. Opposite signs. Signed, -2 < 5 gives -1; unsigned, 4294967294 > 5 gives 1.
  4. Range of one. Comparing only index 0 of each array compares 7 with 7, so the result is exactly 0.

Frequently Asked Questions

It compares two integer arrays lexicographically, like Arrays.compare, but reads every element as an unsigned number.
When the arrays hold raw binary data, such as byte keys, hashes, or IP addresses, where 0xFF should be larger than 0x01.
byte[], short[], int[], and long[]. char[] is already unsigned, and floating-point types have no unsigned form.
Java 9. On Java 8, compare element by element with Byte.toUnsignedInt or Integer.compareUnsigned.
Only when a negative element is involved at the first difference. For arrays of non-negative values the two give the same sign.

Next: Arrays.copyOf()

Copy an array into a new one that is longer or shorter.

copyOf() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

10 people found this page helpful