Java Arrays.copyOf() Method

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

Definition

In Java, Arrays.copyOf(original, newLength) creates a new array of length newLength and copies elements from the start of original into it.

If the new length is shorter, the extra elements are cut off. If it is longer, the new slots are filled with the default value: 0 for numbers, false for boolean, '\u0000' for char, and null for objects. Since an array can never change its own length, this is the standard way to "resize" one: copy it and use the new array.

A static method of java.util.Arrays (Java 6+): call it on the class, as in Arrays.copyOf(...); there is no Arrays object to create.

Syntax

Pass the source array and the length you want.

Java
int[] a = {1, 2, 3};
int[] longer = Arrays.copyOf(a, 5);    // [1, 2, 3, 0, 0]
int[] shorter = Arrays.copyOf(a, 2);   // [1, 2]
int[] same = Arrays.copyOf(a, a.length); // [1, 2, 3], a separate array
CallWhat it does
Arrays.copyOf(a, a.length)A full, independent copy.
Arrays.copyOf(a, a.length * 2)Double the room, the usual way to grow a backing array.
Arrays.copyOf(a, n)Keep only the first n elements.
Arrays.copyOf(objs, n, String[].class)Copy into an array of a different (compatible) type.

There is one overload for each primitive type (int[], long[], short[], byte[], char[], float[], double[], boolean[]), a generic T[] version, and a version that takes the target array type.

Library declarations
Java
// java.util.Arrays (since Java 6)
public static int[] copyOf(int[] original, int newLength)
public static double[] copyOf(double[] original, int newLength)
public static boolean[] copyOf(boolean[] original, int newLength)
public static <T> T[] copyOf(T[] original, int newLength)
public static <T, U> T[] copyOf(U[] original, int newLength, Class<? extends T[]> newType)
// also long[], short[], byte[], char[], float[]

Parameters

ParameterDescription
originalThe array to copy. Must not be null.
newLength (int)Length of the new array. Zero is allowed; a negative value is not.
newType (optional)The class of the array to create, such as String[].class.

Return Value

Return ValueDescription
New arrayA new array of length newLength, of the same type as original (or newType), never the original array itself.

Compare with Similar Methods

Other ways to copy an array:

MethodHow it differs
Arrays.copyOfRange(a, from, to)Copies a slice that does not have to start at index 0.
a.clone()Same-length copy only; cannot grow or shrink.
System.arraycopy(...)Copies into an array you already have, at any position.
Arrays.fill(a, v)Use after copying to give the padding slots a value other than the default.

Java Examples

Example 1 — Grow an Array to Add More Values

An array of three numbers is full. Copy it into a bigger one, then fill the new slots.

Java
import java.util.Arrays;

public class ArraysCopyOfDemo {
    public static void main(String[] args) {
        int[] nums = {7, 8, 9};
        int[] bigger = Arrays.copyOf(nums, 5);
        System.out.println("bigger:   " + Arrays.toString(bigger));

        bigger[3] = 10;
        bigger[4] = 11;
        System.out.println("filled:   " + Arrays.toString(bigger));
        System.out.println("original: " + Arrays.toString(nums));
    }
}

How It Works

  1. A new, longer array. copyOf(nums, 5) allocates five slots and copies 7, 8, and 9 into the first three.
  2. Default padding. Slots 3 and 4 had nothing to copy, so they hold the int default, 0.
  3. Use the new slots. Assigning bigger[3] and bigger[4] works because bigger really has length 5.
  4. The source is unchanged. nums still has length 3; to "resize" a variable you assign the copy back, as in nums = Arrays.copyOf(nums, 5).

Example 2 — Shorter and Longer Copies of Different Types

Cut a String[] down, pad it with null, and see the padding for boolean and double.

Java
import java.util.Arrays;

public class ArraysCopyOfPadding {
    public static void main(String[] args) {
        String[] names = {"Ava", "Bo", "Cal", "Dee"};
        System.out.println(Arrays.toString(Arrays.copyOf(names, 2)));
        System.out.println(Arrays.toString(Arrays.copyOf(names, 6)));

        boolean[] flags = {true};
        double[] prices = {9.5};
        System.out.println(Arrays.toString(Arrays.copyOf(flags, 3)));
        System.out.println(Arrays.toString(Arrays.copyOf(prices, 3)));
    }
}

How It Works

  1. Truncation keeps the front. A new length of 2 keeps Ava and Bo and drops Cal and Dee.
  2. Objects pad with null. Growing the four names to six adds two null references at the end.
  3. Booleans pad with false. The boolean[] copy keeps true and adds two false values.
  4. Doubles pad with 0.0. The double[] overload uses the double default, printed as 0.0.

Example 3 — A Tiny Growable Buffer and a Negative Length

Double the array whenever it is full, trim it at the end, then try a negative length.

Java
import java.util.Arrays;

public class ArraysCopyOfGrowable {
    public static void main(String[] args) {
        int[] data = new int[2];
        int size = 0;
        for (int v = 1; v <= 5; v++) {
            if (size == data.length) {
                data = Arrays.copyOf(data, data.length * 2);
                System.out.println("grew to " + data.length);
            }
            data[size++] = v * 10;
        }
        System.out.println(Arrays.toString(Arrays.copyOf(data, size)));

        try {
            Arrays.copyOf(data, -1);
        } catch (NegativeArraySizeException e) {
            System.out.println("negative length rejected");
        }
    }
}

How It Works

  1. Full means grow. When size reaches data.length, the array is replaced by a copy twice as long: 2 becomes 4 (before storing 30), then 4 becomes 8 (before storing 50).
  2. Count separately. size tracks how many slots are really used, because data.length is the capacity, not the number of values.
  3. Trim at the end. copyOf(data, size) drops the three unused slots and leaves exactly the five values. This is roughly what ArrayList does internally.
  4. Negative length. An array cannot have length -1, so the call throws NegativeArraySizeException before copying anything.

Frequently Asked Questions

You cannot change an existing array. Create a longer copy with Arrays.copyOf(a, newLength) and assign it back: a = Arrays.copyOf(a, a.length + 1).
The default for the element type: 0 or 0.0 for numbers, false for boolean, the null character for char, and null for objects.
No. For object arrays it copies the references, so both arrays share the same element objects. For primitive arrays the values themselves are copied.
Arrays.copyOf throws NegativeArraySizeException.
clone() always makes a copy of the same length. Arrays.copyOf lets you choose a shorter or longer length.

Next: Arrays.copyOfRange()

Copy a slice of an array, from one index up to another.

copyOfRange() tutorial →

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.

11 people found this page helpful