This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
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

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
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
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
subject: shallow copy vs deep copy?
Similar Threads
shallow copy and deep copy
need your help about a cast about cloneable.!!!(jcjp07-Q119)
Diffarence between shallow copy and deep copy
Cloneable and deep copy.
What is the difference between the deep copy and shallow copy in Object.clone()