100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace

What is a Shallow Copy?

Shallow copy explained — how reference fields stay shared, Object.clone() defaults, and shared-mutation bugs — with a Java code example.

mediumQ100 of 226 in Object Oriented Programming Est. time: 5 minsLast updated:
Open Code Lab

Expected Interview Answer

A shallow copy creates a new object and copies the values of the original object’s fields into it, but for any field that is itself a reference to another object, only the reference (the address) is copied — both the original and the copy end up pointing to the exact same nested object.

Because a shallow copy duplicates only the top-level structure, primitive fields (int, double, boolean) are truly independent between the original and the copy, but reference-type fields (arrays, other objects, collections) remain shared. Mutating a shared nested object through the copy is visible through the original too, since there is really only one such nested object in memory with two paths leading to it. In Java, Object.clone()'s default behavior is a shallow copy, and languages commonly provide it as the cheap, fast default because it avoids recursively duplicating an entire object graph. Shallow copying is appropriate when nested objects are immutable or intentionally meant to be shared, but is a common source of subtle bugs when a developer assumes independence that does not actually exist.

  • Fast and cheap — no recursive duplication of nested objects
  • Correct and sufficient when nested fields are immutable or shared intentionally
  • Simple default behavior (e.g. Object.clone() in Java)
  • Avoids unnecessary memory duplication for large object graphs

AI Mentor Explanation

Photocopying a scorecard duplicates the paper itself perfectly, but if the scorecard has a sticky note attached referencing “the official ball log” instead of copying its contents, both the original and the photocopy still point to the exact same shared ball log — scribble a correction on it via either copy and both scorecards reflect the change. That is a shallow copy: the top-level sheet is duplicated, but any referenced sub-record stays shared, not duplicated.

Step-by-Step Explanation

  1. Step 1

    Allocate a new object

    A new object instance is created, separate in memory from the original.

  2. Step 2

    Copy primitive fields by value

    Fields like int, double, boolean are duplicated as independent values.

  3. Step 3

    Copy reference fields by reference

    Fields pointing to other objects (arrays, collections, nested objects) copy only the reference, not the target.

  4. Step 4

    Original and copy share nested state

    Both objects now point to the same nested object(s); mutating one is visible through the other.

What Interviewer Expects

  • Clear distinction between primitive and reference field copying
  • Awareness that Object.clone() in Java defaults to shallow copy semantics
  • Ability to identify when shallow copying causes a bug (shared mutable nested state)
  • Knowledge of when shallow copying is acceptable (immutable nested fields)

Common Mistakes

  • Assuming clone() always produces a fully independent object
  • Not realizing arrays and collections are reference types subject to shallow copying
  • Mutating a nested object through a copy and being surprised the original changed
  • Confusing shallow copy with a deep copy performance optimization

Best Answer (HR Friendly)

A shallow copy makes a new object and copies over the simple values directly, but for any field that points to another object, it just copies the pointer, so both the original and the copy end up sharing that same nested object. That is fine if the nested data never changes, but it becomes a bug source if someone mutates the shared nested object through the copy and expects the original to stay unaffected.

Code Example

Shallow copy shares nested references
class Address {
    String city;
    Address(String city) { this.city = city; }
}

class Person implements Cloneable {
    String name;      // primitive-like: copied by value
    Address address;  // reference type: copied by reference in a shallow copy

    Person(String name, Address address) {
        this.name = name;
        this.address = address;
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone(); // default Object.clone() is a shallow copy
    }
}

Person original = new Person("Asha", new Address("Pune"));
Person copy = (Person) original.clone();
copy.address.city = "Mumbai";
System.out.println(original.address.city); // prints "Mumbai" - shared reference!

Follow-up Questions

  • How does a deep copy differ from a shallow copy in what it duplicates?
  • What does Object.clone() do by default in Java?
  • When is shallow copying actually the correct choice?
  • How would you implement a proper deep copy for a class with nested objects?

MCQ Practice

1. In a shallow copy, reference-type fields are copied by?

A shallow copy only duplicates the reference (pointer), so the original and copy share the same nested object.

2. What is the default behavior of Object.clone() in Java?

Object.clone()'s default implementation performs a field-by-field shallow copy.

3. A shallow copy is a reasonable choice when?

Shallow copying is safe and efficient when nested objects are immutable or meant to be shared on purpose.

Flash Cards

Shallow copy in one line?New object with primitives duplicated but reference fields still pointing to the same nested objects.

What happens if you mutate a nested object via the copy?The original also reflects the change, since the nested object is shared.

Java default clone() behavior?Shallow copy — Object.clone() copies fields but not what references point to.

When is shallow copy safe?When nested fields are immutable or intentionally shared between instances.

1 / 4

Continue Learning