skip to content

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