What is the difference between shallow copy and deep copy in Java?

When we copy an object, a shallow copy is where only the outer object is copied. For example, if we have an object named sample and it contains another object named subSample then if you copy this only the contents of the sample are copied.

When we copy an object deep copy is where whole object structure is copied i.e. if you copy sample the contents of both sample and subSample were copied.

Swarali Sree
Swarali Sree

I love thought experiments.