InterviewSolution
| 1. |
What are shallow copy and deep copy in java? |
|
Answer» To copy the object's data, we have several methods like deep copy and shallow copy. Example - class Rectangle{INT length = 5; int BREADTH = 3;}Object for this Rectangle class - Rectangle obj1 = new Rectangle();
Now by doing this what will happen is the new reference is created with the name obj2 and that will point to the same memory location.
Both these objects will point to the memory location as stated below - Now, if we change the values in shallow copy then they affect the other reference as well. Let's see with the help of an example - class Rectangle{int length = 5; int breadth = 3;}public class Main{public static void main(String[] args) { Rectangle obj1 = new Rectangle(); //Shallow Copy Rectangle obj2 = obj1; System.out.println(" Before Changing the value of object 1, the object2 will be - "); System.out.println(" Object2 Length = "+obj2.length+", Object2 Breadth = "+obj2.breadth); //Changing the values for object1. obj1.length = 10; obj1.breadth = 20; System.out.println("\N After Changing the value of object 1, the object2 will be - "); System.out.println(" Object2 Length = "+obj2.length+", Object2 Breadth = "+obj2.breadth); }}Output - Before Changing the value of object 1, the object2 will be - Object2 Length = 5, Object2 Breadth = 3After Changing the value of object 1, the object2 will be - Object2 Length = 10, Object2 Breadth = 20We can see that in the above code, if we change the values of object1, then the object2 values also get changed. It is because of the reference. Now, if we change the code to deep copy, then there will be no effect on object2 if it is of type deep copy. Consider some SNIPPETS to be added in the above code. class Rectangle{ int length = 5; int breadth = 3;}public class Main{public static void main(String[] args) { Rectangle obj1 = new Rectangle(); //Shallow Copy Rectangle obj2 = new Rectangle(); obj2.length = obj1.length; obj2.breadth = obj1.breadth; System.out.println(" Before Changing the value of object 1, the object2 will be - "); System.out.println(" Object2 Length = "+obj2.length+", Object2 Breadth = "+obj2.breadth); //Changing the values for object1. obj1.length = 10; obj1.breadth = 20; System.out.println("\n After Changing the value of object 1, the object2 will be - "); System.out.println(" Object2 Length = "+obj2.length+", Object2 Breadth = "+obj2.breadth); }}The above snippet will not affect the object2 values. It has its separate values. The output will be Before Changing the value of object 1, the object2 will be - Object2 Length = 5, Object2 Breadth = 3After Changing the value of object 1, the object2 will be - Object2 Length = 5, Object2 Breadth = 3Now we see that we need to write the number of codes for this deep copy. So to reduce this, In java, there is a method called clone(). The clone() will do this deep copy internally and return a new object. And to do this we need to write only 1 line of code. That is - Rectangle obj2 = obj1.clone(); |
|