Java Arrays clone() Explained: Deep Dive with Examples & Best Practices
Source: Dev.to
What Exactly is the clone() Method for Arrays?
The key thing to remember right off the bat: for arrays, clone() performs a shallow copy.
Shallow Copy vs. Deep Copy: The Core Concept
- Shallow Copy – A new array object is created, but its elements are references to the same objects stored in the original array. Modifying a mutable object through one array is reflected in the other.
- Deep Copy – A completely independent duplicate of the original array and of every object it contains. Changes to one do not affect the other.
For primitive arrays (int[], char[], …) the distinction is irrelevant because the array stores the actual values. In that case clone() effectively gives you an independent copy.
For object arrays (String[], Employee[], …) the array stores references, so a shallow copy duplicates only those references.
Let’s Get Our Hands Dirty: Code Examples
Example 1 – Primitive Array
import java.util.Arrays;
public class CloneDemo {
public static void main(String[] args) {
int[] originalScores = {95, 87, 92, 64};
int[] clonedScores = originalScores.clone();
System.out.println("Original Array: " + Arrays.toString(originalScores));
System.out.println("Cloned Array: " + Arrays.toString(clonedScores));
// Change the cloned array
clonedScores[0] = 100;
System.out.println("\nAfter modifying cloned array:");
System.out.println("Original Array: " + Arrays.toString(originalScores)); // Still 95
System.out.println("Cloned Array: " + Arrays.toString(clonedScores)); // Now 100
}
}
Output
Original Array: [95, 87, 92, 64]
Cloned Array: [95, 87, 92, 64]
After modifying cloned array:
Original Array: [95, 87, 92, 64]
Cloned Array: [100, 87, 92, 64]
Example 2 – The Gotcha! (Object Array)
class Player {
String name;
int level;
Player(String name, int level) {
this.name = name;
this.level = level;
}
}
public class CloneGotcha {
public static void main(String[] args) {
Player[] originalTeam = {
new Player("Alex", 10),
new Player("Sam", 15)
};
Player[] clonedTeam = originalTeam.clone();
System.out.println("Before change:");
System.out.println("Original[0]: " + originalTeam[0].level); // 10
System.out.println("Cloned[0]: " + clonedTeam[0].level); // 10
// Modify the object referenced by the cloned array
clonedTeam[0].level = 99;
System.out.println("\nAfter changing clonedTeam[0].level to 99:");
System.out.println("Original[0]: " + originalTeam[0].level); // Oops! 99!
System.out.println("Cloned[0]: " + clonedTeam[0].level); // 99
}
}
Output
Before change:
Original[0]: 10
Cloned[0]: 10
After changing clonedTeam[0].level to 99:
Original[0]: 99
Cloned[0]: 99
Real‑World Use Cases: Where Does clone() Shine?
Defensive Copying in Getters
public class Configuration {
private String[] settings;
public String[] getSettings() {
return settings.clone(); // Caller cannot modify the internal array
}
}
As a Building Block for Deep Copy
clone() can be the first step in a more complex deep‑copy routine, after which you manually copy the mutable objects contained in the array.
Best Practices & Pitfalls to Avoid
- Assume shallow copy for object arrays. If the array holds mutable objects,
clone()alone is rarely sufficient. - Perform a manual deep copy when you need independent objects. Example for the
Playerarray:
Player[] deepCopyTeam = new Player[originalTeam.length];
for (int i = 0; i (original)` or `list.toArray()` for copying.
-
Q: When should I absolutely not use
clone()?
A: When you need a deep copy of an array containing mutable objects, or when you want clearer intent; alternatives likeArrays.copyOfor manual copying are safer. -
Q: What about performance?
A:clone()is a native array copy and is generally fast for shallow copies. Deep copies require additional work and will be slower proportionally to the amount of data you duplicate.
Conclusion: Should You Use It?
- Use
clone()for primitive arrays, arrays of immutable objects (String,Integer, etc.), or whenever a shallow copy meets your needs. - Avoid
clone()when you require independent copies of mutable objects. - Consider alternatives such as
Arrays.copyOf,System.arraycopy, or explicit loops for clearer intent and better control over deep‑copy semantics.
Understanding how clone() works—and its limitations—is essential for writing correct and efficient Java code.