Java Arrays.mismatch() Method

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

Definition

In Java, Arrays.mismatch(a, b) walks both arrays from index 0 and returns the index of the first position where they differ. If the arrays are equal, it returns -1.

If one array is shorter and every element it has matches the start of the other, the first "difference" is the missing element, so the result is the length of the shorter array. That also makes mismatch a quick way to measure the length of a common prefix.

A static method of java.util.Arrays (Java 9+): call it on the class, as in Arrays.mismatch(...); 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, loop from index 0 until a[i] != b[i] or the shorter length is reached; Arrays.equals only tells you whether a difference exists.

Syntax

Pass two arrays of the same type.

Java
int[] a = {1, 2, 3, 4};
Arrays.mismatch(a, new int[] {1, 2, 9, 4});   // 2
Arrays.mismatch(a, new int[] {1, 2});         // 2 (shorter array's length)
Arrays.mismatch(a, a.clone());                // -1 (equal)
CallWhat it does
Arrays.mismatch(a, b)First differing index, or -1.
Arrays.mismatch(a, aFrom, aTo, b, bFrom, bTo)Searches two ranges; the result is relative to the range starts.
Arrays.mismatch(x, y, comparator)Object elements differ when the comparator does not return 0.
Arrays.mismatch(a, b) == -1Same answer as Arrays.equals(a, b) for non-null arrays.

Overloads exist for every primitive type, boolean[], Object[] (elements compared with Objects.equals), and T[] with a Comparator, each with a range form.

Library declarations
Java
// java.util.Arrays (since Java 9)
public static int mismatch(int[] a, int[] b)
public static int mismatch(int[] a, int aFromIndex, int aToIndex,
                           int[] b, int bFromIndex, int bToIndex)
public static int mismatch(Object[] a, Object[] b)
public static <T> int mismatch(T[] a, T[] b, Comparator<? super T> cmp)
// also boolean[], byte[], short[], char[], long[], float[], double[]

Parameters

ParameterDescription
a, bThe arrays to compare. Neither may be null.
range indexes (optional)From (inclusive) and to (exclusive) indexes for each array.
cmp (optional)Comparator that decides when two object elements match.

Return Value

Return ValueDescription
-1The arrays (or ranges) have the same length and equal elements.
int ≥ 0Index of the first difference; equals the shorter length when one is a prefix of the other.

Compare with Similar Methods

Methods that compare arrays:

MethodHow it differs
Arrays.equals(a, b)Only says whether there is a difference.
Arrays.compare(a, b)Uses the first difference to decide which array comes first.
Arrays.deepEquals(a, b)Equality for nested arrays; mismatch is shallow.
Arrays.binarySearch(a, key)Finds a value's position in one sorted array.

Java Examples

Example 1 — Find Where a Result Went Wrong

Compare an expected sequence with an actual one and report the first wrong value.

Java
import java.util.Arrays;

public class ArraysMismatchDemo {
    public static void main(String[] args) {
        int[] expected = {4, 8, 15, 16, 23, 42};
        int[] actual = {4, 8, 15, 61, 23, 42};

        int i = Arrays.mismatch(expected, actual);
        System.out.println("first difference at index " + i);
        System.out.println("expected " + expected[i] + " but got " + actual[i]);
        System.out.println("copy: " + Arrays.mismatch(expected, expected.clone()));
    }
}

How It Works

  1. Matching start. Indexes 0 to 2 hold 4, 8, and 15 in both arrays.
  2. First difference. At index 3, 16 does not equal 61, so mismatch returns 3 and stops looking.
  3. Use the index. Reading expected[i] and actual[i] gives a useful message, the kind a test framework prints.
  4. No difference. A clone has identical contents, so the result is -1; always check for -1 before using the result as an index.

Example 2 — Common Prefix of Words as char Arrays

Measure how many leading letters two words share, including a word that is a prefix of another.

Java
import java.util.Arrays;

public class ArraysMismatchPrefix {
    public static void main(String[] args) {
        char[] a = "interview".toCharArray();
        char[] b = "internet".toCharArray();
        char[] c = "inter".toCharArray();

        int ab = Arrays.mismatch(a, b);
        System.out.println("a vs b: " + ab + " -> common prefix \"" + new String(a, 0, ab) + "\"");
        System.out.println("a vs c: " + Arrays.mismatch(a, c));
        System.out.println("c vs c: " + Arrays.mismatch(c, c));
    }
}

How It Works

  1. Letters as an array. toCharArray() gives each word as a char[], so the char[] overload is used.
  2. Prefix length. "interview" and "internet" share i, n, t, e, r and differ at index 5 ('v' vs 'n'), so 5 is also the length of the common prefix.
  3. Shorter array runs out. "inter" matches the first five letters of "interview" and then ends, so the result is its length, 5, not -1.
  4. Same array. Comparing c with itself finds no difference and returns -1.

Example 3 — null Elements and Range Comparison

Compare String[] arrays that contain null, then compare the tail of one log with another.

Java
import java.util.Arrays;

public class ArraysMismatchRange {
    public static void main(String[] args) {
        String[] left = {"x", null, "y", "z"};
        String[] right = {"x", null, "y", "Z"};
        System.out.println("full arrays: " + Arrays.mismatch(left, right));

        int[] log1 = {0, 0, 7, 8, 9};
        int[] log2 = {7, 8, 1};
        System.out.println("log1[2..5) vs log2[0..3): " + Arrays.mismatch(log1, 2, 5, log2, 0, 3));
    }
}

How It Works

  1. null matches null. Object elements are compared with Objects.equals, so the two null values at index 1 count as equal.
  2. Case-sensitive strings. "z" and "Z" are different strings, so the first difference is at index 3.
  3. Two ranges. The range form compares {7, 8, 9} from log1 with {7, 8, 1} from log2.
  4. Relative index. The difference is at position 2 of each range (array index 4 in log1), and 2 is what is returned.

Frequently Asked Questions

-1. Any other result is the index of the first difference.
If the shorter array matches the start of the longer one, mismatch returns the length of the shorter array.
No. A null array throws NullPointerException. null elements inside object arrays are fine.
Java 9. On Java 8, write a loop that stops at the first index where the elements differ.
Relative: it counts from the start of each range, so add aFromIndex to get the index in the first array.

Next: Arrays.parallelPrefix()

Replace each element with a running total (or running max, product, ...).

parallelPrefix() 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