Java Arrays Class

Beginner
8 min read
Updated: Nov 2026
3 examples
java.util.Arrays

Definition and Usage

In Java, java.util.Arrays is a utility class: a collection of static methods that do common jobs on arrays. It can print an array with toString(), sort it with sort(), search it with binarySearch(), copy it with copyOf(), fill it with fill(), and compare two arrays with equals().

The class is final and its constructor is private, so you cannot create an Arrays object; you always call Arrays.methodName(...). Most methods come in one version per primitive array type (int[], long[], double[], and so on) plus one for object arrays. The Arrays Methods index lists every guide.

Arrays have a fixed length.

Once you create new int[5], it holds exactly five elements forever. No Arrays method can make that array longer or shorter: copyOf() and copyOfRange() return a new array, and asList() gives a list whose size is also fixed. For a growable collection, use an ArrayList.

Java Syntax

Import the class, then pass your array to a static method:

Java
import java.util.Arrays;

int[] a = {3, 1, 2};
Arrays.sort(a);                              // a is now 1, 2, 3
String text = Arrays.toString(a);            // "[1, 2, 3]"
int[] b = Arrays.copyOf(a, 5);               // [1, 2, 3, 0, 0]
boolean same = Arrays.equals(a, b);          // false (lengths differ)
// Arrays x = new Arrays();                  // does not compile: private constructor

Methods that rearrange or overwrite elements, such as sort(), fill(), and setAll(), change the array you pass in and return void. Methods that build something new, such as copyOf() and toString(), leave the original alone.

Array vs Arrays vs ArrayList

Three names that look alike but mean different things:

NameWhat it isKeep in mind
int[], String[]A plain array: fixed length, fast index access, can hold primitivesNo useful toString(), equals(), or hashCode() of its own
java.util.ArraysA helper class with static methods that work on arraysNot a container; you never create one
ArrayListA resizable list object backed by an internal arrayHolds objects only, so numbers become Integer, Double, and so on

Why Printing an Array Looks Strange

Arrays inherit toString(), equals(), and hashCode() from Object. So System.out.println(nums) prints something like [I@1b6d3586 (a type code plus a hash that changes between runs), nums1.equals(nums2) is true only for the very same array object, and nums.hashCode() ignores the contents. Arrays provides the content-based versions:

Methods Added in Java 8 and Java 9

Most Arrays methods are old enough to work everywhere. These newer ones need a recent JDK:

VersionMethods
Java 8setAll(), parallelSetAll(), parallelSort(), parallelPrefix(), spliterator(), stream()
Java 9compare(), compareUnsigned(), mismatch(), plus range and Comparator overloads of equals()

Java Examples

Example 1 — Print, Sort, Search, and Slice an Array

A small set of test scores: print them, sort them in place, find one score, and copy out the top two.

Java
import java.util.Arrays;

public class ArraysHubBasics {
    public static void main(String[] args) {
        int[] scores = {88, 42, 75, 99, 60};
        System.out.println("original: " + Arrays.toString(scores));

        Arrays.sort(scores);
        System.out.println("sorted:   " + Arrays.toString(scores));
        System.out.println("75 is at index " + Arrays.binarySearch(scores, 75));

        int[] top = Arrays.copyOfRange(scores, 3, 5);
        System.out.println("top two:  " + Arrays.toString(top));
    }
}

How It Works

  1. Readable printing. Arrays.toString(scores) builds the text [88, 42, 75, 99, 60]; printing scores by itself would only show a type code and a hash.
  2. Sorting changes the original. Arrays.sort returns nothing; it rearranges the five numbers inside scores from smallest to largest.
  3. Search only after sorting. binarySearch relies on the sorted order and finds 75 at index 2.
  4. Slice into a new array. copyOfRange(scores, 3, 5) copies indexes 3 and 4 (the end index is excluded), which are the two highest scores.

Example 2 — Fill, Copy, and Compare Arrays

Build a row of dots, change a copy of it, compare the two, and print a 2D grid.

Java
import java.util.Arrays;

public class ArraysHubCopyCompare {
    public static void main(String[] args) {
        char[] row = new char[4];
        Arrays.fill(row, '.');
        char[] copy = Arrays.copyOf(row, row.length);
        copy[2] = '#';

        System.out.println(new String(row) + " vs " + new String(copy));
        System.out.println("equal? " + Arrays.equals(row, copy));

        int[][] grid = { {1, 2}, {3, 4} };
        System.out.println(Arrays.deepToString(grid));
    }
}

How It Works

  1. One value everywhere. A new char[4] starts with four '\u0000' characters; Arrays.fill(row, '.') replaces all four with dots.
  2. An independent copy. Arrays.copyOf(row, row.length) creates a second array, so setting copy[2] to '#' leaves row untouched.
  3. Content comparison. Arrays.equals walks both arrays position by position and reports false because index 2 differs.
  4. Nested arrays. A 2D array is an array of rows, so Arrays.deepToString is needed to print the numbers inside each row.

Example 3 — From a Fixed Array to a List and a Stream

View an array as a List, copy it into a growable ArrayList, and add up numbers with a stream.

Java
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class ArraysHubToList {
    public static void main(String[] args) {
        String[] fixed = {"red", "green"};
        List<String> view = Arrays.asList(fixed);
        List<String> list = new ArrayList<>(view);
        list.add("blue");

        System.out.println("array length " + fixed.length + ", view " + view + ", list " + list);

        int[] nums = {4, 5, 6};
        System.out.println("sum via stream: " + Arrays.stream(nums).sum());
    }
}

How It Works

  1. A list view. Arrays.asList(fixed) wraps the two-element array in a List without copying it, and that list cannot grow.
  2. Copy to grow. Passing the view to new ArrayList<>(...) copies the strings into a real ArrayList, which accepts add("blue").
  3. The array is unchanged. fixed.length is still 2: adding to the ArrayList never resizes the original array.
  4. Numbers as a stream. Arrays.stream(nums) gives an IntStream, and sum() adds 4 + 5 + 6 to get 15.

Next: Java Arrays Methods by Group

Full list with short descriptions: Java Arrays Methods index (/java/arrays/method).

Frequently Asked Questions

java.util.Arrays is a utility class with static methods for arrays: toString, sort, binarySearch, copyOf, fill, equals, hashCode, asList, stream, and more. It has been part of Java since version 1.2, and newer versions keep adding methods.
No. The class has a private constructor and every method is static, so you call methods on the class name, for example Arrays.sort(a). Writing new Arrays() does not compile.
Arrays is a helper class for plain arrays such as int[] and String[], which have a fixed length. ArrayList is a list object that grows and shrinks as you add and remove elements. Arrays.asList and new ArrayList<>(Arrays.asList(...)) connect the two.
Use Arrays.toString(a) for a one-dimensional array and Arrays.deepToString(a) for nested arrays. Printing the array directly shows something like [I@1b6d3586, which is a type code and a hash, not the contents.
Not directly: the Comparator overloads only accept object arrays. Either sort an Integer[] with Collections.reverseOrder(), or sort the int[] ascending and then reverse it with a simple loop.

Browse every Arrays method

Open the methods index for categorized guides with examples and FAQs.

Arrays Methods Index →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful