Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...

what is object deep copy and shallow copy and why it is
required?

Answer Posted / sanjay

Java provides a mechanism for creating copies of objects
called cloning.
There are two ways to make a copy of an object called
1. shallow copy
2. deep copy

Shallow copy is a bit-wise copy of an object. A new
object is created that has an exact copy of the values in
the original object. If any of the fields of the object are
references to other objects, just the references are copied.
Thus, if the object you are copying contains references to
yet other objects, a shallow copy refers to the same subobjects.

Deep copy is a complete duplicate copy of an object.
If an object has references to other objects, complete new
copies of those objects are also made. A deep copy generates
a copy not only of the primitive values of the original
object, but copies of all subobjects as well, all the way to
the bottom. If you need a true, complete copy of the
original object, then you will need to implement a full deep
copy for the object.
Java supports shallow and deep copy with the
Cloneable interface to create copies of objects. To make a
clone of a Java object, you declare that an object
implements Cloneable, and then provide an override of the
clone method of the standard Java Object base class.
Implementing Cloneable tells the java compiler that your
object is Cloneable. The cloning is actually done by the
clone method.

Is This Answer Correct ?    11 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

How many types of keywords are there?

990


What is the difference between quicksort & mergesort? When should they be used? What is their running time?

1085


For class CFoo { }; what default methods will the compiler generate for you>?

1068


Can we sort hashmap in java?

953


Can an interface implement another interface?

1025


How do you control extraneous variables?

914


What is an exception in java?

1105


What is the purpose of void?

991


what is use of functional interface in java 8?

1010


Implement a stack with push (), pop() and min() in O(1) time.

1104


What are the main concepts of oops in java?

1732


Is arraylist zero based?

1059


How do you create a bulleted list?

1253


What is difference between equals and hashcode method?

1126


What does compareto () do in java?

990