0Pricing
Java Academy · Lesson

Arrays.equals and toString

Compare and print arrays.

Arrays.equals and toString is a free Java Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Java Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Comparing and Printing

Arrays do not override equals or toString in a useful way. The Arrays utility class provides equals and toString that actually inspect element contents.

Why == Fails

Using == on arrays compares references, not contents. Two different arrays with identical elements are not == equal.

public class Main {
    public static void main(String[] args) {
        int[] a = {1, 2, 3};
        int[] b = {1, 2, 3};
        System.out.println(a == b);
    }
}

Arrays.equals

Arrays.equals(a, b) returns true when both arrays have the same length and equal elements in the same positions.

import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        int[] a = {1, 2, 3};
        int[] b = {1, 2, 3};
        System.out.println(Arrays.equals(a, b));
    }
}

Order Matters

Element order is significant. Two arrays with the same values in a different order are not equal.

import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        int[] a = {1, 2, 3};
        int[] b = {3, 2, 1};
        System.out.println(Arrays.equals(a, b));
    }
}

Comparing Object Arrays

For object arrays, Arrays.equals compares elements using their own equals method.

import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        String[] a = {"x", "y", "z"};
        String[] b = {"x", "y", "z"};
        System.out.println(Arrays.equals(a, b));
    }
}

toString for Readable Output

Printing an array directly shows something like [I@1b6d3586, the class name and hash. Arrays.toString produces a readable [1, 2, 3] form.

import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        int[] nums = {1, 2, 3};
        System.out.println(nums);
        System.out.println(Arrays.toString(nums));
    }
}

Nested Arrays: deepEquals

For arrays of arrays, plain equals only checks the outer references of each row. Use Arrays.deepEquals to compare nested contents.

import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        int[][] a = {{1, 2}, {3, 4}};
        int[][] b = {{1, 2}, {3, 4}};
        System.out.println(Arrays.equals(a, b));
        System.out.println(Arrays.deepEquals(a, b));
    }
}

Nested Arrays: deepToString

Likewise, Arrays.deepToString prints nested arrays clearly, while plain toString would show inner references.

import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        int[][] grid = {{1, 2}, {3, 4}};
        System.out.println(Arrays.toString(grid));
        System.out.println(Arrays.deepToString(grid));
    }
}

hashCode for Arrays

If you need a content-based hash, use Arrays.hashCode (and Arrays.deepHashCode for nested arrays). These are consistent with Arrays.equals.

import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        int[] a = {1, 2, 3};
        int[] b = {1, 2, 3};
        System.out.println(Arrays.hashCode(a) == Arrays.hashCode(b));
    }
}

Comparing After Sorting

To check whether two arrays contain the same multiset of values regardless of order, sort copies of both and then compare with Arrays.equals.

import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        int[] a = {3, 1, 2};
        int[] b = {2, 3, 1};
        int[] ca = Arrays.copyOf(a, a.length);
        int[] cb = Arrays.copyOf(b, b.length);
        Arrays.sort(ca);
        Arrays.sort(cb);
        System.out.println(Arrays.equals(ca, cb));
    }
}

Logging Made Easy

During debugging, wrap arrays in Arrays.toString inside print statements so you can actually read the values instead of cryptic references.

Quick Check

Test your understanding of comparing arrays.

Recap

You learned to compare and print arrays.

  • == compares references; use Arrays.equals for content equality.
  • Order and length both matter.
  • Arrays.toString gives readable output; deepToString handles nesting.
  • deepEquals compares nested arrays.

Frequently asked questions

Is the “Arrays.equals and toString” lesson free?

Yes — the full text of “Arrays.equals and toString” is free to read here on the web, and the Java Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Java Academy course, upgrade to CoddyKit PRO.

What will I learn in “Arrays.equals and toString”?

Compare and print arrays. You practise Java Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Java Academy?

No prior experience is required. Java Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Arrays.equals and toString” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Java Academy lesson?

Yes. Every Java Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Arrays.sort and Sorting
  2. Arrays.binarySearch
  3. Arrays.fill and copyOf
  4. Arrays.equals and toString
← Back to Java Academy