Java ProgramsArraysCompare Two Arrays

Compare Two Arrays in Java

beginner·  Arrays  ·  Array Manipulation

Problem

Two arrays are considered equal when they have the same length and every element matches at the same position.

Given two arrays of integers, determine whether they are equal.

Input
[4, 8, 15, 16], [4, 8, 15, 16]
Output
Arrays are equal: true

Java Program

Java
public class CompareTwoArrays { public static void main(String[] args) { int[] arr1 = {4, 8, 15, 16}; int[] arr2 = {4, 8, 15, 16}; boolean equal = arr1.length == arr2.length; if (equal) { for (int i = 0; i < arr1.length; i++) { if (arr1[i] != arr2[i]) { equal = false; break; // mismatch found, no need to keep checking } } } System.out.println("Arrays are equal: " + equal); } }

Output

Arrays are equal: true

Core Logic

Checking the lengths first, then comparing every position one by one, confirms whether the two arrays truly match.

How It Works
  1. 1arr1.length == arr2.length is checked first — arrays of different lengths can never be equal.
  2. 2If the lengths match, a loop compares arr1[i] against arr2[i] at every index.
  3. 3The first mismatch found sets equal to false and exits the loop immediately with break.
  4. 4If every position matches, equal stays true for the whole scan.
For [4, 8, 15, 16] and [4, 8, 15, 16], every position matches, so equal stays true.
💡

Key Point: arr1 == arr2 would compare object references, not contents — two separate arrays holding identical values would still report false with ==, which is why this checks each element individually instead.

Complexity
Time Complexity: O(n)Space Complexity: O(1)

Why: Each position is checked once, and the loop exits at the first mismatch found, without allocating anything beyond a boolean flag.

Key Concepts

for loopearly exit with breakarray length

Approach 2: Using Arrays.equals()

Java
import java.util.Arrays; public class CompareTwoArraysBuiltin { public static void main(String[] args) { int[] arr1 = {4, 8, 15, 16}; int[] arr2 = {4, 8, 15, 16}; // Arrays.equals() checks both length and every element in one call boolean equal = Arrays.equals(arr1, arr2); System.out.println("Arrays are equal: " + equal); } }

Output

Arrays are equal: true

Core Logic

In real code, there's no reason to write the comparison loop yourself — Arrays.equals() already checks both length and contents in one call.

How It Works
  1. 1Arrays.equals(arr1, arr2) takes both arrays and returns a single boolean.
  2. 2Internally, it performs the same length check and element-by-element comparison as the manual loop.
  3. 3No explicit loop is needed in your own code.
Arrays.equals(new int[]{4, 8, 15, 16}, new int[]{4, 8, 15, 16}) returns true in a single call.
💡

Key Point: This is the version to actually use — the manual loop exists only to show what Arrays.equals() is conceptually doing under the hood.

Complexity
Time Complexity: O(n)Space Complexity: O(1)

Why: Arrays.equals() still performs the same kind of element-by-element scan internally, but that scan happens inside the JDK instead of your own loop.

Key Concepts

Arrays.equals()

Related Programs