skip to content

Arrays Utility Methods

java.util.Arrays gives you sort, binarySearch (which requires a sorted input), fill, copyOf, stream, and the equals versus deepEquals pair. Arrays.asList is the trap: fixed size and backed by the original array.

part ofJavaoverview, primer and where to startread it →
on this pageshow

questions

6

What is java.util.Arrays, and what kinds of operations does it provide for working with arrays?

level: juniorimportance: must knowfreq 60%

answer

  1. Static helper class — never instantiated
  2. sort / binarySearch / fill / copyOf / equals / toString / asList / stream
  3. Overloaded for every primitive + Object
  4. Fixes array's bad default toString and ==

basics

~20 s

java.util.Arrays is a helper class full of static methods for arrays: it can sort, search, fill, copy, compare, print, and turn an array into a list or stream. You call them like Arrays.sort(a) without creating an Arrays object.

solid answer

~40 s

java.util.Arrays is a utility (helper) class of static methods that bundle the most common array chores so you don't write them by hand. The headline groups are: sorting (sort), searching a sorted array (binarySearch), bulk-setting values (fill), copying with resize (copyOf, copyOfRange), comparing element-by-element (equals, deepEquals), producing readable text (toString, deepToString), bridging to collections/streams (asList, stream), and hashing (hashCode, deepHashCode). All methods are static, so you never instantiate Arrays; you call Arrays.method(...). They are overloaded for every primitive type plus Object, so the same name works for int[], double[], String[], and so on. Knowing this class prevents reinventing loops and avoids subtle bugs like using == on arrays.

go deeper

for a junior

Knows Arrays is a static helper class and can name a few methods like sort and toString and use them correctly.

for a middle

Explains the method families, knows methods are overloaded per type, and reaches for Arrays instead of hand-rolling loops.

for a senior

Articulates why the class exists (arrays' bare API, bad default toString/equals) and chooses the right method for printing, comparing, and copying multi-dimensional arrays.

for a principal

Frames Arrays within the broader collections/streams story, weighs it against newer constructs (List.copyOf, streams) and guides teams on idiomatic, bug-free array handling.

## What an array is An **array** in Java is a fixed-length container of values of one type, e.g. `int[] a = {3, 1, 2}`. Once created, its length never changes. Arrays are objects, but they are low-level: they have a `.length` field and `[]` indexing, and almost no methods of their own. ## What `java.util.Arrays` is `java.util.Arrays` is a **utility class** (also called a helper class): a class that holds only **static methods** — methods that belong to the class itself, not to an instance. You call them as `Arrays.sort(a)`, never by doing `new Arrays()`. Its job is to provide the everyday operations the bare array type lacks. ## The main families of methods - **Sorting:** `Arrays.sort(a)` orders elements ascending; an overload `sort(a, comparator)` (for object arrays) or `sort(a, fromIndex, toIndex)` sorts a range. - **Searching:** `Arrays.binarySearch(a, key)` finds an element fast — but **only works on an already-sorted array**. - **Filling:** `Arrays.fill(a, value)` sets every slot to one value. - **Copying / resizing:** `Arrays.copyOf(a, newLength)` makes a new array of a chosen length (padding or truncating); `copyOfRange(a, from, to)` copies a slice. - **Comparing:** `Arrays.equals(a, b)` compares two arrays element-by-element (because `==` only checks if they are the *same object*). `deepEquals` handles nested arrays. - **Printing:** `Arrays.toString(a)` gives `[3, 1, 2]`; `deepToString` handles nested arrays. - **Bridging:** `Arrays.asList(...)` views an array as a `List`; `Arrays.stream(a)` starts a stream pipeline. - **Hashing:** `Arrays.hashCode(a)` / `deepHashCode(a)` compute content-based hash codes. ## Why it matters Without these helpers you would hand-write loops for printing, comparing, and copying — error-prone and verbose. More importantly, the **default behaviors of arrays surprise people**: `array.toString()` prints something like `[I@1b6d3586` (type + hash), and `array1 == array2` compares identity, not contents. `Arrays.toString` and `Arrays.equals` fix both. Every method is **overloaded** for the eight primitive types and for `Object[]`, so the same call shape works regardless of element type. ## Mental model Think of `Arrays` as the 'standard library toolbox' bolted onto a type (arrays) that the language itself left almost bare. Whenever you are about to write a manual loop over an array to sort, search, copy, compare, or print it, there is almost certainly an `Arrays` method that does it correctly.

  • Why does printing an array with System.out.println(myArray) give something like [I@1b6d3586?
    Arrays don't override toString(), so Object.toString() runs, printing the type descriptor ([I = int array) plus the identity hash code. Use Arrays.toString(myArray) to print the contents.
  • Are the Arrays methods generic or overloaded?
    Mostly overloaded per primitive type (int[], long[], double[]...) plus an Object[]/generic version. Java can't make one method body work for primitive arrays via generics, so they are duplicated per type.

saying these in an interview costs you the question

  • Thinking you must create an Arrays instance (new Arrays())
  • Believing arrays have rich built-in methods themselves (they only have .length and [])
  • Confusing java.util.Arrays with java.lang.reflect.Array

context

open as a page

How does Arrays.binarySearch work, what precondition must hold, and what does it return when the key is absent?

level: middleimportance: must knowfreq 65%

basics

~20 s

Arrays.binarySearch quickly finds a value in a sorted array. The array MUST already be sorted, or results are wrong. If found, it returns the index. If not found, it returns a negative number: -(insertionPoint) - 1.

open as a page

What are the pitfalls of Arrays.asList — its fixed-size backing behavior and how it handles a primitive array?

level: seniorimportance: must knowfreq 55%

basics

~20 s

Arrays.asList wraps an array as a List, but it's a fixed-size view backed by the array: you can't add or remove (those throw), and changes write through to the array. Also, asList(int[]) gives a List<int[]> with one element, not a List<Integer>; use a boxed Integer[] or a stream.

open as a page

How do Arrays.copyOf and Arrays.copyOfRange work, what happens at the boundaries, and is the copy deep or shallow?

level: middleimportance: should knowfreq 45%

basics

~20 s

Arrays.copyOf makes a new array of a chosen length, copying the original and padding with defaults (0/null/false) if longer, or cutting off if shorter. copyOfRange copies a slice [from, to). Both make a shallow copy: object elements are shared, not cloned.

open as a page

What is the difference between Arrays.equals and Arrays.deepEquals (and likewise toString vs deepToString), and when must you use the deep version?

level: middleimportance: should knowfreq 50%

basics

~20 s

Arrays.equals compares two arrays one element at a time, but for arrays of arrays it only checks if the inner arrays are the same object. Arrays.deepEquals goes inside nested arrays and compares their contents too. Same idea for toString (one level) vs deepToString (nested).

open as a page

How does Arrays.sort behave for primitives vs objects (algorithm, stability, custom order), and what does Arrays.stream / parallelSort add?

level: seniorimportance: should knowfreq 48%

basics

~20 s

Arrays.sort orders an array ascending in place. For objects it's a stable merge-style sort and you can pass a Comparator for custom order; for primitives it's an unstable quicksort and there's no comparator. Arrays.stream turns an array into a stream for map/filter/reduce; parallelSort sorts using multiple threads.

open as a page