What are the benefits of using identical String literals instead of a final variable?

前端 未结 6 1565
迷失自我
迷失自我 2020-12-08 19:25

I\'ve come across a class that includes multiple uses of a string literal, \"foo\".

What I\'d like to know, is what are the benefits and impact (in terms of object c

6条回答
  •  时光取名叫无心
    2020-12-08 20:17

    From the JLS
    Compile-time constants of type String are always "interned" so as to share unique instances, using the method String.intern.

    So, no, there's gonna be only one string object.

    As Mark notes, this is strictly the question of maintainability and not performance.

提交回复
热议问题