Shallow Copy in Java

Last Updated : 30 Jun, 2026

A Shallow Copy in Java creates a new object while copying all the field values from the original object. Primitive fields are copied by value, whereas non-primitive fields (objects, arrays, etc.) are copied as references. As a result, both the original and copied objects share the same nested objects, so changes to those shared objects are reflected in both.

  • Non-primitive fields are copied by reference.
  • Original and copied objects share the same nested objects.
  • Changes to shared objects affect both copies.
shallowCopy

Shallow Cloning

Shallow Cloning is the process of creating a shallow copy of an object using the clone() method. It duplicates the object itself but does not create separate copies of the referenced objects. This is the default behavior of Object.clone() in Java.

  • Copies object references instead of cloning nested objects.
  • Faster and uses less memory than deep cloning.

Types of Data Copied in Shallow Copy

  • Primitive fields: The primitive fields that are int, float, etc. are copied by value. This means changes to one variable do not affect to other.
  • Non-primitive fields: The non-primitive fields that are objects, arrays, etc. are copied by reference. This means the original and copied object will share the same reference to these fields, and changes made in the copy will affect the original.

Example 1: In this example, we will perform shallow copy using the clone() method.

Java
class Address {
    String city;

    Address(String city) { 
        this.city = city; 
    }
}

class Person implements Cloneable {
    String name;
    Address addr;

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

    @Override
    protected Object clone() throws CloneNotSupportedException {
      
        // Performs shallow copy 
        // using clone method
        return super.clone();
    }
}

public class Geeks {
    public static void main(String[] args) 
      throws CloneNotSupportedException {
      
        // Create Address object
        Address a = new Address("London");

        // Create Person object
        Person p1 = new Person("A", a);

        // Create a shallow copy of p1
        Person p2 = (Person)p1.clone();
      
        // Displaying the cities to show shallow copy behavior
        System.out.println("Original City: " + p1.addr.city);
        System.out.println("Copied City: " + p2.addr.city);

        // Modify the copied object's address 
        p2.addr.city = "Paris";  

        // Observe the changes
        System.out.println("After change:");
        System.out.println("Original City: " + p1.addr.city); 
        System.out.println("Copied City: " + p2.addr.city);   
    }
}

Output
Original City: London
Copied City: London
After change:
Original City: Paris
Copied City: Paris

Explanation: In the above example, the Person object is shallow-copied. This means the addr field is shared between the original and the copied object. Any changes made to p2.addr.city also affect p1.addr.city because they reference the same Address object.

Example 2: Below Java program demonstrates shallow copy with primitives and nested objects.

Java
class Address {
    String city;
    Address(String city) { 
      this.city = city; 
    }
}

class Person implements Cloneable {
  
    // Primitive field
    int age; 
  
    // Nested object
    Address addr; 

    Person(int age, Address addr)
    {
        this.age = age;
        this.addr = addr;
    }

    @Override
    protected Object clone()
        throws CloneNotSupportedException
    {
        // Shallow copy
        return super.clone(); 
    }
}

public class Geeks {
    public static void main(String[] args)
        throws CloneNotSupportedException
    {
        Address a = new Address("London");
      
        // Original object
        Person p1 = new Person(30, a); 

        // Create a shallow copy of Person
        Person p2 = (Person)p1.clone();
      
        // Displaying original and copied values
        System.out.println("Original Age: " + p1.age);
        System.out.println("Copied Age: " + p2.age);
        System.out.println("Original City: " + p1.addr.city);
        System.out.println("Copied City: " + p2.addr.city);

        // Modify primitive field and nested object 
        // in the copied object
        p2.age = 35; 
        p2.addr.city = "Paris"; 

        System.out.println("After modification:");
        System.out.println("Original Age: " + p1.age); 
        System.out.println("Copied Age: " + p2.age); 
        System.out.println("Original City: " + p1.addr.city);
        System.out.println("Copied City: " + p2.addr.city); 
    }
}

Output
Original Age: 30
Copied Age: 30
Original City: London
Copied City: London
After modification:
Original Age: 30
Copied Age: 35
Original City: Paris
Copied City: Paris

Explanation: In this example, the primitive field age is copied by value, so changes in the copied object don't affect the original. But the nested Address object is copied by reference, so modifications in the copied object also affect the original.

Advantages of Shallow Copy

  • Fast Object Creation: Copies objects quickly because nested objects are not duplicated.
  • Memory Efficient: Uses less memory by sharing references to existing objects.
  • Simple to Implement: Easily created using the clone() method or copy constructors.
  • Improves Performance: Avoids the overhead of copying complex object graphs.
  • Suitable for Immutable Objects: Works well when referenced objects cannot be modified.
  • Useful for Lightweight Duplication: Ideal when shared references between objects are acceptable.
Comment