Strings vs classes when both are reference types

后端 未结 4 2046
北荒
北荒 2021-02-02 00:07

Here is how my last interview went:

Question: Where are strings stored?

Answer: Heap since it is a reference type

4条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-02-02 01:01

    The command one = ... sets the *pointer named one to a new value. Since two is a different pointer, it retains it's original value.

    The command one.someString = ... modifies the object referred by one. two is still a different pointer, but since it points to the same object, the modifications are shared between them.

提交回复
热议问题