File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes shallow copy vs deep copy? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "shallow copy vs deep copy?" Watch "shallow copy vs deep copy?" New topic

shallow copy vs deep copy?

Aman Verma

Joined: May 10, 2005
Posts: 15
Can any one plz explain what is shallow copy and deep copy? and how does it relate with clone() method?

Thanks in advance,
Chit Ming Chong
Ranch Hand

Joined: Sep 09, 2004
Posts: 49
shallow copy copies only the reference. deep copy copies the content referrenced. You can override the clone method to do a shallow or deep copy although a deep copy is generally assumed.
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
To explain a little further. Imagine if you haad an ArrayList full of JButtons. With a shallow copy, you would have two ArrayLists but still the same number of Jbutton objects. With a deep copy, the ArrayList would be copied and so would all the items in the ArrayList. So if you started with 20 JButtons in one ArrayList you would now have 40 JButton objects in two ArrayLists.

Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
I agree. Here's the link:
subject: shallow copy vs deep copy?
jQuery in Action, 3rd edition