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

前端 未结 6 1580
迷失自我
迷失自我 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

    All String literals are kept in a String cache (this is across all classes)

    Using a constant can make the code clearer, give the the string some context and make the code easier to maintain esp if the same string appears in multiple places.

提交回复
热议问题