Is string interning really useful?

前端 未结 7 2041
北恋
北恋 2020-12-30 00:26

I was having a conversation about strings and various languages a while back, and the topic of string interning came up. Apparently Java and the .NET framework do this auto

7条回答
  •  无人及你
    2020-12-30 00:50

    String interning is useful when you need to compare strings (1) from a finite set (2) a number of times.

    Then the overhead of interning a string is outweighed by the benefit of being able to do a fast == instead of equals().

    Doing this can sometimes be faster than using a HashMap, which relies on hashCode() and equals() calls.

提交回复
热议问题