DEV Community

Cover image for Shallow & Deep Copy In Java
Rahul Raj
Rahul Raj

Posted on

Shallow & Deep Copy In Java

Shallow Copy :

  • Shallow copy is a technique in which we have more than one reference variable pointing to the same object, means here we try to copy the reference of the object.

  • If we change anything in the object with the help of any one of the reference variable then that change will be reflected to all the reference variables.

  • Here we try to copy reference of object not creating a new object .

Shallow Copy Example

//ELC
class Test
{
    public static void  main(String []args){
        Employee emp1 = new Employee(10000);
        System.out.println(emp1.getSalary()); //
        System.out.println(); //

        Employee emp2 = emp1 ; 

        emp2.setSalary(20000); 
        System.out.println(emp1.getSalary()); //
        System.out.println(emp2.getSalary()); //
    }
}

// BLC class 
class Employee
{
    double salary;
    Employee(double salary){
        this.salary=salary;
    }   
    public double getSalary(){
        return this.salary;
    }
    public void setSalary(double salary){
        this.salary = salary;
    }
}


Enter fullscreen mode Exit fullscreen mode

shallow copy

shallow copy

Deep Copy

  • Deep copy is a technique in which always try to create a new Object .
  • Generally we create new object with the help of new Keyword .
  • new keyword is dynamic memory allocation operator .
  • new keyword always create a new object .

//ELC
class Test
{
    public static void  main(String []args){
    Employee emp1 = new Employee();
    Employee emp2 = new Employee(2000);
    emp1.setSalary(emp2.getSalary());
    System.out.println(emp1.getSalary());
    System.out.println(emp2.getSalary());
    System.out.println();
    emp1.setSalary(80000);
    System.out.println(emp1.getSalary());
    System.out.println(emp2.getSalary());

    }
}

// BLC class 
class Employee
{
    double salary;
    Employee(){}
    Employee(double salary){
        this.salary=salary;
    }   
    double getSalary(){
        return this.salary;
    }
    void setSalary(double salary){
        this.salary = salary;
    }
}

Enter fullscreen mode Exit fullscreen mode

deep copy

Pass By Value :

  • we have to type of variable .
  1. Primitive variable
  2. Reference variable

case 1 : when we pass Primitive Variable

  • when we pass primitive variable then we pass actual value .

class Test
{
    public static void  main(String []args){
Test t1 = new Test();
    int x = 10;
    System.out.println(x);
    t1.display(x);
    System.out.println(x);  
    }
    void display(int y ){
        y = y+10;
        }
}

Enter fullscreen mode Exit fullscreen mode

primitive value

case 2 : when we pass Reference Variable

  • when we pass Reference variable then we pass Reference of Object not actual object .
  • Means here we try to copy reference of object not creating Object .
  • It is look like Shallow Copy concept ..

Top comments (0)