str1
and str2
are pointers that reference the same area of memory. Your memory layout looks roughly like this:

If you change where str1
points, e.g., by doing this
str1 = @"new string";
then str2
will still reference "matrix", but str1
will reference "new string":

Let's say, though, that str1
and str2
actually pointed to an instance of an NSMutableString
, and you did this instead:
[str2 setString:@"new string"];
Note, then, that str1
and str2
would still point the same object, so by modifying str2
, str1
would also change to "new string".
Shallow copy vs. deep copy
A shallow copy is a copy of an object in which its instance variables still point to the same memory location as the original object's ivars. A deep copy is a copy in which copies of the instance variables are also made.
Let's say you have a class, MyClass
, that has two instance variables, each of type NSString
. Here's a diagram of what the memory layout would roughly look like after a shallow and a deep copy:
