Will the memory addresses of s1 & s2 be same?
maybe -- implementation is defined by Foundation.
If Yes, then Why?
the implementation may return the parameter when and if no copy needs to be made -- the object you request behaves identical to the input parameter when the input parameter is an immutable NSString
. when passing the immortal NSString
literal as you have done, the implementation could easily determine that the parameter is immutable, and opt to return the parameter (retained
).
of course, that's an optimization the library (Foundation) would need to recognize and support.
it's also possible that the implementation may elect not to return the input parameter, due to variances in implementation details. as one example, the implementation may return a new string if the string were created using an externally owned character buffer. it may also recognize the parameter is mutable, then would of course need to return a new immutable object representation.
What if both strings are mutable?
then deep copying is more probable -- implementation is defined by Foundation. however, you will have unique instances (their addresses will not match).