Computer Science
Confusing Shallow Copy and Deep Copy
A student writes b = a.copy() and expects changes inside b's nested objects to leave a unchanged.
Quick check
Why students make it
The misleading intuition is that copying a container also copies everything it points to. In many languages, a copy operation only duplicates one level unless a deep-copy operation is requested.
The rule done right
A shallow copy creates a new outer container but keeps references to the same nested objects. A deep copy recursively copies nested objects so independent mutation of nested state does not affect the original.
Related resources
- AP CSA Java Reference Sheet Cheat Sheet
- Modern C++ Syntax Quick Reference Cheat Sheet
- Data Structures Big-O and Operation Costs Cheat Sheet
- Object-Oriented Programming, Classes and Objects Worksheet
- CS, Arrays and Lists Worksheet
- CS, Debugging, Finding and Fixing Errors Worksheet
- CS, Functions and Parameters Worksheet
- Class Design & Object Reference Explorer Tool
- Java Array & ArrayList Explorer Tool
- Java Strings & Methods Explorer Tool
- Debugging & Unit Testing Explorer Tool
- OOP & Class Design Lab Lab
- Arrays & ArrayList Lab Lab
- Debugging & Unit Testing Lab Lab
- Selection & Iteration Code Tracing Lab Lab