Why only literal strings saved in the intern pool by default?

前端 未结 3 991
[愿得一人]
[愿得一人] 2020-12-03 02:37

Why by default only literal strings are saved in the intern pool?

Example from MSDN:

String s1 = \"MyTest\";
String s2 = new StringBuilder().Append(\         


        
3条回答
  •  佛祖请我去吃肉
    2020-12-03 03:24

    The language designers decided the cost of interning every intermediate string value was not worth the performance cost. Interning of garbage-collectible strings requires a single global weak map which can become a bottleneck when you have large numbers of threads.

提交回复
热议问题