Arrays.mismatch compares two arrays element by element and returns the index of the first difference, or -1 if one is a prefix of the other or they are identical, which is faster than writing a manual loop.
int[] left = {1, 2, 3, 9, 5};
int[] right = {1, 2, 3, 4, 5};
System.out.println("First mismatch: " + Arrays.mismatch(left, right));
int[] same = {1, 2, 3};
System.out.println("Identical arrays: " + Arrays.mismatch(same, same));
First mismatch: 3
Identical arrays: -1
Run this yourself in the Online Java Compiler, spin up a live REST API in the API Sandbox, or practise with Java interview questions.
Published 2026-09-27