copying objects Flashcards
reference copy
the new object in the stack references the object in the heap and its attributes
shallow copy
the new object in the stack references an new object in the heap, but its references point to the original objects attributes
deep copy
the new object in the stack references a completely new object with a new copy of the attributes
when to use reference…
pros: fasted, least amount of space used
cons: if class is mutable, aliasing must be avoided (need true copies to prevent privacy leaks and modifications from outside)
when to use shallow…
pros: faster than deep copy, less space used than deep copy
cons: still has aliases (bad, only if the class is mutable)
when to use deep…
pros: no aliasing (necessary for mutable classes)
cons: slowest, takes up the most space