Every object goes through a lifecycle. It's created by a constructor (its birthday), it lives while your program uses it, and it's destroyed when it's no longer needed (or when a destructor runs in languages like C++).
In some languages (like Python and Java), a garbage collector automatically cleans up dead objects. In others (like C++), you must clean up yourself β otherwise you get a memory leak, which is like never throwing out your trash.
A value type (like an integer or a small struct) holds the actual data right inside the variable. A reference type (like most objects) holds a pointer to where the data lives somewhere else.
This matters because copying a value type copies the data β but copying a reference type copies only the pointer. Two variables can point to the same object, and changing one changes both!
The stack is fast and organized: it stores local variables and the pointers to objects. It works like a stack of plates β last in, first out.
The heap is larger and messier: it stores the actual object data. Objects on the heap live longer and are managed by the garbage collector (or by you, in C++). Understanding both is the key to writing fast, safe code.
Sometimes you need a second copy of an object. But not all copies are equal:
A shallow copy copies only the top-level fields β nested objects are shared. A deep copy copies everything, including nested objects. A move (in C++) transfers ownership without copying β much faster when you don't need the original anymore.
When an object is no longer used, someone has to clean it up. Two common strategies:
Reference counting keeps a tally of how many pointers point to an object. When the count hits zero, the object is deleted. Tracing garbage collection periodically scans memory to find unreachable objects and removes them.
Each has tradeβoffs: reference counting is fast but can't handle cycles; tracing GC handles cycles but can pause your program.
Are two objects the same thing or just look the same? That's the difference between identity and equality.
Identity asks: "Is this the exact same object in memory?" (often written is in Python or == in Java for references). Equality asks: "Do these two objects have the same data?" (usually equals() or __eq__).
Hashing assigns a number to an object so it can be found quickly in a dictionary or hash map. If two objects are equal, their hashes must match β that's a rule every programmer must follow!
Lifecycle tracker: Write a simple class with a constructor and destructor (or __init__ and __del__ in Python). Create and destroy an object, and print a message at each stage. Watch the lifecycle happen live!
Value vs. reference test: Create two variables holding a simple integer, change one, and observe. Then do the same with a list (reference type). Explain why the results differ.
Shallow vs. deep copy: Create a nested list, make a shallow copy with copy.copy() and a deep copy with copy.deepcopy(). Modify the nested list and see which copy is affected.
Identity detective: Create two lists with the same contents. Use is and == (or id()) to check their identity and equality. Explain the results in one sentence.
Hash hunt: Create a custom class and implement __eq__ and __hash__. Put instances in a set and a dictionary. Verify that duplicates are caught correctly.
Memory map (bonus): Use Python's id() function or Java's System.identityHashCode() to print the "address" of several objects. Notice how they differ.
By the end of this module, you'll be able to: explain how objects are created and destroyed, distinguish value from reference types, describe stack vs. heap memory, choose between shallow/deep/move copies, and reason about identity, equality, and hashing.