In Java, Shallow copy means creating a clone of an object, where the object itself is copied but the nested objects are not copied. Instead of duplicating the nested objects, a shallow copy only copies the reference to those nested objects. This means that both the original object and the copied object share the same references to the nested objects. The same change is reflected in the copied object.
Shallow Cloning
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 program to demonstrate shallow copy
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 program to demonstrate shallow copy
// with primitives and nested objects
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.
Key Points
- The original and the shallow copy share references to the same objects. This means changes to nested objects (non-primitive fields) in one will be reflected in both the original and the copied object.
- If an object contains references to mutable objects, modifying those referenced objects will affect both the original and the shallow copy.
- Primitive fields are copied by value. Therefore, changes to primitive fields in one object do not affect the other object.
- To do exact object copy we use Deep copy