JavaThe Arrays Class

The Java Arrays Class

Why a Utility Class?

Arrays themselves don't come with built-in methods for sorting, searching, printing, or comparing — an array is a very bare-bones structure. Instead, Java provides java.util.Arrays, a utility class full of static helper methods that operate on arrays. You never create an instance of Arrays — you just call its methods directly, like Arrays.sort(...).

Java
import java.util.Arrays;
Arrays.sort() — Sorting in Place

Java
int[] numbers = {5, 2, 8, 1, 9};
Arrays.sort(numbers);
System.out.println(Arrays.toString(numbers));
[1, 2, 5, 8, 9]
Note
Arrays.sort() modifies the array directly (in place) and returns nothing — it doesn't give you back a new sorted array.
Arrays.toString() — Actually Printing an Array

A very common beginner surprise: printing an array directly with println doesn't show its contents at all.

Broken: printing the array directly

Java
int[] numbers = {1, 2, 5, 8, 9};
System.out.println(numbers);
[I@1b6d3586
Warning
That cryptic [I@1b6d3586 is NOT the array's contents — arrays don't override toString(), so println falls back to the default Object representation: the array's type code plus its hash code in hexadecimal. It tells you almost nothing useful. To actually see what's inside, use Arrays.toString() (or Arrays.deepToString() for multidimensional arrays).

Fixed: using Arrays.toString()

Java
int[] numbers = {1, 2, 5, 8, 9};
System.out.println(Arrays.toString(numbers));

int[][] grid = {{1, 2}, {3, 4}};
System.out.println(Arrays.deepToString(grid));
[1, 2, 5, 8, 9]
[[1, 2], [3, 4]]
Arrays.equals() — Comparing Contents

Arrays are objects, so using == on two arrays compares whether they're the SAME object in memory — not whether they contain the same values. Arrays.equals() compares element-by-element content instead.

Java
int[] a = {1, 2, 3};
int[] b = {1, 2, 3};

System.out.println(a == b);              // reference comparison
System.out.println(Arrays.equals(a, b)); // content comparison
false
true
Warning
a == b is false even though a and b hold identical values, because they are two separate array objects at two separate memory locations. This is the same reference-vs-content distinction you should already be careful about with objects generally — always use Arrays.equals() (or .equals() for other object types) to compare contents, and reserve == for checking whether two variables point to the exact same object.
Arrays.fill() — Filling Every Slot

Java
int[] scores = new int[5];
Arrays.fill(scores, 100);
System.out.println(Arrays.toString(scores));
[100, 100, 100, 100, 100]
Arrays.binarySearch() — Fast Lookup on Sorted Arrays

Java
int[] numbers = {1, 2, 5, 8, 9};
int index = Arrays.binarySearch(numbers, 8);
System.out.println("Found at index: " + index);
Found at index: 3
Note
binarySearch only gives a correct result if the array is ALREADY sorted. If you run it on an unsorted array, the result is undefined — sort first with Arrays.sort() if you're not certain.
Arrays.asList() — A Fixed-Size List View

Java
String[] names = {"Amina", "Chen", "Diego"};
List<String> nameList = Arrays.asList(names);
System.out.println(nameList);
[Amina, Chen, Diego]
Warning
Arrays.asList() returns a fixed-size list backed directly by the original array — you can use set() to change elements, but calling add() or remove() throws an UnsupportedOperationException, since that would require resizing the underlying array. If you need a fully resizable list, wrap it: new ArrayList<>(Arrays.asList(names)).
Quick Reference

Method

Purpose

Arrays.sort(arr)

Sorts the array in place, ascending order

Arrays.toString(arr)

Returns a readable [a, b, c] string of contents

Arrays.deepToString(arr)

Like toString(), but for nested/multidimensional arrays

Arrays.equals(a, b)

Compares contents of two arrays for equality

Arrays.fill(arr, value)

Sets every element to the given value

Arrays.binarySearch(arr, value)

Finds the index of value in a SORTED array

Arrays.asList(arr)

Returns a fixed-size List view backed by the array

Practice Exercises
  1. Sort an int array and print it correctly using Arrays.toString().

  2. Compare two arrays with == and Arrays.equals() and explain the different results.

  3. Use Arrays.fill() to initialize a 10-element array with the value -1, then use Arrays.binarySearch() after sorting a separate array to find a specific value.