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
Call
What 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
Parameter
Description
original
The 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 Value
Description
New array
A new array of length newLength, of the same type as original (or newType), never the original array itself.
Truncation keeps the front. A new length of 2 keeps Ava and Bo and drops Cal and Dee.
Objects pad with null. Growing the four names to six adds two null references at the end.
Booleans pad with false. The boolean[] copy keeps true and adds two false values.
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");
}
}
}
Output
grew to 4
grew to 8
[10, 20, 30, 40, 50]
negative length rejected
How It Works
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).
Count separately.size tracks how many slots are really used, because data.length is the capacity, not the number of values.
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.
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.