File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "?(cloning)  from a newbie" Watch "?(cloning)  from a newbie" New topic

?(cloning) from a newbie

Panday Manako
Ranch Hand

Joined: Mar 29, 2001
Posts: 80
What's the difference between cloning and assignment?
int p[] = new int[10];
int p2[] = (int[])p.clone();
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.
I agree. Here's the link:
subject: ?(cloning) from a newbie
It's not a secret anymore!