In this post, we will explore different ways to copy a primitive array in Java, using both built-in methods and custom logic. We will also compare the trade-offs of each approach.

A primitive array stores elements of a primitive type, such as int, char, boolean, etc. It is different from an object array, which store elements of an object type, such as String, Integer, Boolean, etc. In this post, we will focus on copying primitive arrays. To find how to copy object arrays, refer to this article.

1. Using Arrays.copyOf() method

The Arrays.copyOf() method copies the specified number of elements from an array into a new array of the same type, and returns the new array. To copy the full array, we need to pass the length of the original array. For example, we can create a copy of an int array by using Arrays.copyOf() method as follows:

Download  Run Code

 
This method does not require us to create the destination array beforehand, and automatically creates a new array object with the same type and content as the original array.

2. Using Arrays.copyOfRange() method

We can also use the Arrays.copyOfRange() method for copying an array in Java. This method is similar to Arrays.copyOf(), but instead of copying the whole array or the elements from the beginning, it copies a specified range of elements from the original array into a new array. It takes three parameters: the original array, the starting position (inclusive), the ending position (exclusive). We can use it to copy the whole array by taking the starting index as 0 and ending index equal to the original array’s length.

Download  Run Code

 
This method provides more flexibility and functionality than Arrays.copyOf(), as it allows us to specify which part of the original array we want to copy.

3. Using System.arraycopy() method

Another way to copy a primitive array in Java is to use the System.arraycopy() method. It copies a specified range of elements from one array to another. It takes five parameters: source array, starting position in the source array, destination array, starting position in the destination array, number of elements to be copied, in that order. We can use it to copy the whole array, as follows:

Download  Run Code

 
This method allows us to specify which part of the original array we want to copy and where we want to place it in the destination array. However, it require us to create the destination array beforehand and has a complex sytax.

4. Using clone() method

The clone() method is inherited from the Object class and creates a shallow copy of the array object, with the same size and content as the original array. For primitive arrays, shallow copy is not a problem, since primitive types are immutable and cannot be changed. We can call it as follows to create a copy of an int array:

Download  Run Code

 
This method is the simplest and most widely used ways to copy a primitive array in Java, being very fast and concise.

5. Using Custom Logic

The last way to copy a primitive array in Java is to use custom logic. We can use a simple for loop to copy elements from the source array to the destination array, one at a time. For example:

 
This method gives us more control and flexibility over the copying logic. However, it is more verbose and error-prone than using built-in methods.

That’s all about copying a primitive array in Java.