Typecasting is required as the clone method returns Object type, whereas Copy Constructor does not need such typecasting. In step 5, we have updated the values of a and b by using the reference variable n, In the existing memory, the values of a and b are replaced with 50 and 40 respectively as shown in the memory.
Let's see a simple example program step by step to understand the concept.
In step 6, when we created a new object of the class, a new memory is allocated by JVM but we did not get updated values in the new memory as shown in the above figure because JVM did not copy the updated data into the new memory. Take a look. As sweet was having true value for m1, m2's sweet will be having true value too.
Key points: 1. Next, when we have changed the value of sweet for object m1 from true to false and called flavor method for m1, it shows the result as "Mango is not sweet".
Now, you will get updated data inside the new memory location with the help of a copy constructor. So, we can say that a copy constructor is a constructor which is used for copying the data from one memory location object to another memory location new object meanwhile object creation.