When should we use intern method of String on String literals

后端 未结 14 961
生来不讨喜
生来不讨喜 2020-11-22 08:11

According to String#intern(), intern method is supposed to return the String from the String pool if the String is found in String pool, otherwise a new string

14条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-22 08:25

    string intern() method is used to create an exact copy of heap string object in string constant pool. The string objects in the string constant pool are automatically interned but string objects in heap are not. The main use of creating interns is to save the memory space and to perform faster comparison of string objects.

    Source : What is string intern in java?

提交回复
热议问题