Reusability of Strings in java?

后端 未结 4 749
野趣味
野趣味 2020-12-18 22:59
String h = \"hi\";

here we are referencing string h to string literal hi. JVM has a string literal pool to store string literals so we can reuse t

4条回答
  •  忘掉有多难
    2020-12-18 23:50

    What is meant is that if 20 objects use the same literal String:

    private String h = "hi";
    

    all these objects will in fact reference the same String instance in memory. And it doesn't matter, because it's impossible to change the content of the String, since it's immutable. The same instance can thus be shared without problems between objects.

提交回复
热议问题