Skip to main content
Documents
Share
LinkedIn
Facebook
Twitter
Copy
Java keywordsIntroduction To JavaJava File HandlingJava Language BasicsJava ArraysJava Object-Oriented Programming

Java Array copyOf()

The Arrays.copyOf() method in Java is a utility function provided by the java.util.Arrays class. It is used to create a new array by copying the specified range of elements from an existing array. This method is useful for array resizing, partial array copying, and creating backups of arrays.

Usage

Arrays.copyOf() is commonly used when you need to duplicate an array with a different size or when you want to copy only a portion of an array. It returns a new array containing the copied elements, and the original array remains unchanged.

Syntax

public static <T> T[] copyOf(T[] original, int newLength)
  • original: The array from which elements are to be copied.
  • newLength: The length of the new array. If it is greater than the length of the original array, the new array is padded with default values.

Examples

Example 1: Copying an Entire Array

import java.util.Arrays;

public class CopyOfExample {
    public static void main(String[] args) {
        int[] originalArray = {1, 2, 3, 4, 5};
        int[] copiedArray = Arrays.copyOf(originalArray, originalArray.length);
        
        System.out.println("Original Array: " + Arrays.toString(originalArray));
        System.out.println("Copied Array: " + Arrays.toString(copiedArray));
    }
}

In this example, Arrays.copyOf() is used to create an exact copy of originalArray. The new array copiedArray contains all elements from the original array.

Example 2: Copying with a Larger Size

import java.util.Arrays;

public class CopyOfLargerExample {
    public static void main(String[] args) {
        int[] originalArray = {1, 2, 3};
        int[] extendedArray = Arrays.copyOf(originalArray, 5);
        
        System.out.println("Extended Array: " + Arrays.toString(extendedArray));
    }
}

Here, Arrays.copyOf() is used to create a new array extendedArray with a length of 5. The additional elements are initialized to the default value of 0 for the int type.

Example 3: Copying with a Smaller Size

import java.util.Arrays;

public class CopyOfSmallerExample {
    public static void main(String[] args) {
        String[] originalArray = {"Java", "Python", "C++", "JavaScript"};
        String[] truncatedArray = Arrays.copyOf(originalArray, 2);
        
        System.out.println("Truncated Array: " + Arrays.toString(truncatedArray));
    }
}

In this example, Arrays.copyOf() creates a new array truncatedArray with only the first two elements of originalArray.

Tips and Best Practices

  • Immutable Original: The original array is not modified by Arrays.copyOf(), ensuring data integrity.
  • Default Values: When the new array length is greater than the original, the new elements are set to default values (0, null, false, etc.).
  • Array Types: Arrays.copyOf() works with all primitive and object types, making it versatile for various data types.
  • Performance: Be mindful of performance when copying large arrays, as it involves creating a new array and copying elements.

Understanding Shallow and Deep Copy

Arrays.copyOf() produces a shallow copy, where references to the original elements are copied rather than the objects themselves. This is important to remember when dealing with complex data structures like 2D arrays or arrays of objects, where changes to the objects in the original array may reflect in the copied array.

Exceptions

  • NegativeArraySizeException: Thrown if the newLength parameter is negative.
  • NullPointerException: Thrown if the original array is null.