Outdated Drivers Are Slowing You Down
One free scan finds every outdated or missing driver and matches the right update for your exact hardware.Free scan · exact hardware matchWindows Errors? Fix Them Before They Spread
Repair common Windows errors and clear accumulated junk for a smoother, more stable PC - no reinstall needed.Free scan · no reinstallA shallow copy creates a new outer object or array but keeps references to nested objects; a deep copy duplicates the mutable nested state that needs to be independent. Java has no single universal deep-copy operation, so the right approach depends on your object graph and ownership requirements.
What is the difference between a shallow copy and a deep copy?
Think of an object as a box that may contain values and references to other boxes. A shallow copy makes a new outer box and copies its fields as if by assignment. Primitive field values are copied, but a reference field still points to the same referenced object. A deep copy creates independent copies of the relevant mutable objects as well.
| Question | Shallow copy | Deep copy |
|---|---|---|
| Is the outer object new? | Yes | Yes |
| Are nested mutable objects shared? | Usually, yes | No, for the state the copying policy covers |
| Does Java define one automatic operation? | No; several APIs make shallow copies | No; the class or caller must define what is copied |
For example, if an object has a List<String> names field, a shallow copy gets the same list reference. Adding or removing an entry through either object affects the list seen through both. If a copied field refers to an immutable object, sharing it may be safe and intentional.
Does Object.clone() make a deep copy?
No. Oracle’s Java SE 18 Object API specifies that the default clone() operation performs a shallow copy: it copies field contents as if by assignment, without cloning referenced objects.
Object.clone() is protected. A class generally must implement Cloneable for the default implementation to make a field-for-field copy; otherwise, calling it throws CloneNotSupportedException. Cloneable is only a marker interface—it declares no clone() method and does not itself expose a public cloning API. See Oracle’s Java SE 26 Cloneable API.
A class can override clone() and copy nested fields itself, but that means the class author must define and maintain the copy policy. Oracle’s Secure Coding Guidelines for Java SE call the Cloneable mechanism problematic and recommend explicit copy functionality, such as a copy constructor, static creation method, or public copy method for final classes. This is secure-coding guidance, not a Java language prohibition.
Rank #2
What do array-copy methods actually copy?
Primitive arrays
Cloning a primitive array produces a new array containing copied primitive values. The elements themselves are not object references.
Object arrays and Arrays.copyOf
For an object array, cloning or using Arrays.copyOf creates a new outer array but copies the element references. The referenced objects remain shared. When Arrays.copyOf makes a longer reference array, new positions are null. Oracle documents this behavior in the Java SE 26 Arrays API.
Multidimensional arrays
A Java multidimensional array is an array whose elements refer to other arrays. Cloning it creates a new outer array, but its subarrays are shared. The Java Language Specification, Java SE 24, §10.7 explicitly describes this behavior.
How should you copy collections and their elements?
Copying a collection container does not automatically copy its elements. A new list can still hold references to the original mutable objects. Oracle’s secure-coding guidance illustrates creating a new collection and copying mutable Date elements individually. Copy elements only when they need independent state; immutable elements can generally be shared.
Rank #4
There is no universal depth at which a copy must stop. If multiple fields refer to one shared object, decide whether the copied object graph should preserve that internal sharing or give each field its own copy. Cycles also require an explicit strategy so a copier does not recurse indefinitely. These decisions are part of the copy contract, not something a generic copy operation can infer reliably.
Are Java records deeply immutable?
No. Record component fields are final, but an object referenced by a component can still be mutable. Oracle’s Java SE 26 Record API describes records as shallowly immutable and identifies defensive copying of mutable components as a reason to define an explicit canonical constructor or accessor.
Free tools Windows power users keep installed
One-click scans. No signup required.
Best Value
If callers need a record’s mutable list or array to be isolated, define where copying occurs and what the accessor returns. A final reference alone does not prevent changes to the referenced object.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.How do you choose a copy strategy?
- Specify the needed isolation. Decide whether only the outer container must be independent or whether mutable nested state must be independent too.
- Check mutability and ownership. Sharing immutable values is often appropriate; mutable values need copies when either owner could otherwise change state unexpectedly.
- Consider the graph. Identify nested arrays, collections, cycles, and objects shared across fields, then decide which relationships the copy should preserve.
- Make the API policy visible. Prefer a documented copy constructor, static factory, or public copy method when practical, rather than implying that a shallow operation provides deep isolation.
- Maintain the boundary. Update the copy logic when fields or nested types change, and document which state is intentionally shared.
The most useful definition of “deep” is therefore specific to the class: it means independent copies of the mutable state that callers are promised will not be shared—not necessarily a duplicate of every reachable object.
Quick Recap
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

