File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes ?(cloning)  from a newbie Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "?(cloning)  from a newbie" Watch "?(cloning)  from a newbie" New topic
Author

?(cloning) from a newbie

Panday Manako
Ranch Hand

Joined: Mar 29, 2001
Posts: 80
Hi!
What's the difference between cloning and assignment?
example:
int p[] = new int[10];
int p2[] = (int[])p.clone();
VS
int p[] = new int[10];
int p2[] = p;
Thanks in advance.

Siva Prasad
Ranch Hand

Joined: Feb 22, 2001
Posts: 104
in this case there is no difference. Its like copying the reference of one array to another.
Array Objects has their own overriden clone() method, which only does shallow copying. i.e. something like assinging the an array reference to another array reference variable.
Inorder to get your own clone behaviour, you need to write a public method clone() in your class which must call the protected clone() method inherited from Object method.
or you can implement the Cloneable interface.
Cheers
Siva
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: ?(cloning) from a newbie