Strings vs classes when both are reference types

后端 未结 4 2087
北荒
北荒 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:04

    One of reasons strings were made immutable, even though they are reference types, was to make them look and behave like primitive types (e.g., int, double, float).

    That's also the reason why strings are the only reference type that can be represented as a literal (e.g., "some string"). Lots of other languages take the same approach, like Java for example.

提交回复
热议问题