Forums Register Login

simple question

+Pie Number of slices to send: Send
How to perform shallow copy and deep copy using clone() method of object class.
Kindly give simple , precise example of each.
Thanks in advance.
+Pie Number of slices to send: Send
+Pie Number of slices to send: Send
Jeff P
Please note that JavaRanch has very few rules but the one we enforce with vigor is our naming standard (what would a Java programmer be without naming standards )
Please see this page:
http://www.javaranch.com/name.jsp
Please adjust your display name to meet our requirements.
Thanks! And enjoy your visits to the Ranch.
+Pie Number of slices to send: Send
Java performs shallow copies by default. Suppose you have an object, say, Thingamajig, with an instance member object called Thingy. Every Thingamajig object of course has a reference to its Thingy. You can make as many references as you like to the same Thingamajig object:

Both object references t1 and t2 point to the same object, not duplicate objects. Furthermore, both t1 and t2 also have access to the same Thingy object. This is shallow copying.
Deep copying happens when you create identical objects with the same state. If I make a deep copy of the object Thingamajig referred to by t1, I must make a separate instance of its Thingy object, as well as any other objects that the Thingy object itself may have internally, and so on recursively.
Here's a sample code;
I like you because you always keep good, crunchy cereal in your pantry. This tiny ad agrees:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 813 times.
Similar Threads
Migrating a servlet-based application to JBOSS
simple question
How to provide JRE for the product.
What is the difference between 'deep' and 'shallow' binding?
Automatically executing targets on change
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 01:37:47.