Question

In: Computer Science

In JAVA, What is the difference between a shallow copy and a deep copy? Include a...

In JAVA, What is the difference between a shallow copy and a deep copy? Include a simple illustration of the difference.

Solutions

Expert Solution

1. Shallow Copy in Java;

==> When user use default implementation of clone method user get shallow copy of object means it create new instance(object) and copy all the field of object to that new instance and return it as object type we need to explicitly cast it back to our original object.

==> This is shallow copy of the object.

==> clone() method of the object class support shallow copy of the object.

==> The name shallow copy or shallow cloning in Java.

2. Deep Copy in Java

==> When user need own meaning of copy not to use default implementation user call it as deep copy, when user need deep copy of the object user need to implement according to our need.

==> For deep copy user need to ensure all the member class also implement the Cloneable interface and override the clone() method of the object class.

==> After that we override the clone() method in all those classes even in the classes where we have only primitive type members otherwise user would not be able to call the protected clone() method of Object class on the instances of those classes inside some other class

  • In shallow copy, only fields of primitive data type are copied while objects references are not copied.
  • Deep copy involves copy of primitive data type as well as objet references
shallow copy deep copy
  • Shallow copy is fast and also less expensive.
  • Deep copy is slow and very expensive.
  • It is relatively fast
  • It is relatively slow.
  • Default version of clone method creates the shallow copy of an object.
  • To create the deep copy of an object, you have to override clone method.
  • Cloned Object and original object are not 100% disjoint.
  • Cloned Object and original object are 100% disjoint.
  • Cloned object and source object are not disjoint completely
  • Cloned objects and source objects are completely independent of each other.
  • Any changes made to cloned object will be reflected in original object or vice versa.
  • Any changes made to cloned object will not be reflected in original object or vice versa.


Related Solutions

What is meant by the term “globalisation”? What is the difference between “shallow” and “deep integration”?
What is meant by the term “globalisation”? What is the difference between “shallow” and “deep integration”?
Q1: What is the difference between bar drawing and deep drawing?  What is the difference between a...
Q1: What is the difference between bar drawing and deep drawing?  What is the difference between a cutoff operation and a parting operation? Distinguish between direct and indirect extrusion. Although the workpiece in a wire drawing operation is obviously subjected to tensile stresses, how do compressive stresses also play a role in the process? Note: Please write in your own words don't copy and paste.
Consider a shallow-water wave of length 1.5km propagating in an ocean that is 62.5m deep. a)...
Consider a shallow-water wave of length 1.5km propagating in an ocean that is 62.5m deep. a) Calculate the wave’s speed C and period T. (3P) b) Now suppose that this wave propagates into a region in which there is a current of strength U that opposes the wave propagation. Explain how this modifies the dispersion relation and the wave speed. (2P) c) Assuming that the wave’s frequency ω remains constant, create a plot of the wavelength λ as a function...
400 words for each question. no copy and paste What is the difference between operational and...
400 words for each question. no copy and paste What is the difference between operational and analytical CRM? Explain them briefly. Why would a company want to implement an employee relationship management system? Discuss the components in a core ERP system? How does a company measure the success of an ERP system? Briefly explain the three different ERP implementation choices?
pls use deep clone method to make a deep copy of each elements in a singly...
pls use deep clone method to make a deep copy of each elements in a singly linked list. (show detailed code and explanation in java. thanks!)
A swimming pool is 20 ft wide, 40 ft long, 3 ft deep at the shallow...
A swimming pool is 20 ft wide, 40 ft long, 3 ft deep at the shallow end, and 9 ft deep at is deepest point. A cross-section is shown in the figure. If the pool is being filled at a rate of 1.1 ft3/min, how fast is the water level rising when the depth at the deepest point is 5 ft? (Round your answer to five decimal places.)
Q3: Shallow & Deep foundation are the two main types of foundation. Compare how both differs...
Q3: Shallow & Deep foundation are the two main types of foundation. Compare how both differs from each other. Suggest which type is mostly suitable for sky scrapers. Write by microsoft word ( text )
What do you know about programming in Python? What is the difference between Python and Java?
What do you know about programming in Python? What is the difference between Python and Java? What does the term Open Source mean? Name four examples of Open Source software. What is the IDEL programming environment? How does IDEL relate to Python? How do you spread a long statement over multiple lines in Python? How do you use the loop-index? How will knowing and understanding Python impact what you do in your profession and/or personal experiences?
What is the difference between disabilities and handicaps? Include in your answer why it is important...
What is the difference between disabilities and handicaps? Include in your answer why it is important to avoid "labels" when speaking about these individuals. What does intelligence mean? Include in your answer the differences between general, fluid and crystallized intelligence. What is the theory of multiple intelligence? Hint: Your answer cannot be considered complete without mentioning Howard Gardner and the eight intelligences. What is the triarchic theory of successful intelligence? Hint: Your answer cannot be considered complete without mentioning Robert...
Explain the difference between the terms “variable” and “data.” Include an illustration that demonstrates this difference....
Explain the difference between the terms “variable” and “data.” Include an illustration that demonstrates this difference. For Q2 Recall: “Illustration” = “Example”
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT