• Post Reply Bookmark Topic Watch Topic
  • New Topic

What is cloning in java  RSS feed

 
deepak carter
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

What is cloning in java...Didnt fing any good article in google to help me out....A smalll example would help a lot to understand....
 
Joseph Mokenela
Ranch Hand
Posts: 71
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think this example might help you understand better.
 
Raghavendra Shockley
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If deepak does not mind, may i ask why do we need cloning/what are its applications ?
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Raghavendra Shockley wrote:If deepak does not mind, may i ask why do we need cloning/what are its applications ?

Any time you need a copy of an object.

The usual example is an array. Arrays are quite nice, but they have one major problem: they're mutable. So if I simply hand off an array of mine to some nefarious method; it could then alter the contents of my array and screw up my program.

On the other hand, if I hand over a copy of my array, the naughty method can do whatever it likes with the copy, and my array will be untouched.

Does that help?

Winston
 
deepak carter
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
GOT IT.....

Can you explain swallow and deep cloning in the same way.if i can explain like this...i would surely get a good job....


and which one is better swallow or deep
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joseph Mokenela wrote:I think this example might help you understand better.
No, that is a very poor example.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
deepak carter wrote:GOT IT.....

Can you explain swallow and deep cloning in the same way.if i can explain like this...i would surely get a good job....


Shallow cloning is what the clone() method does by default. It makes a copy of the object, but it does not copy any objects that our original object refers to.

Deep cloning is when we make copies of all the objects referred to, and all the objects they refer to, and so on, so that an entire object graph is copied.

and which one is better swallow or deep


Which is better, a hammer or a screwdriver?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!