String Literal Pool
-->
String
allocation, like all object allocation, proves costly in both time
and memory. The JVM performs some trickery while instantiating string
literals
to increase performance and decrease memory overhead. To cut down the
number of String objects created in the JVM, the String class keeps a
pool of strings. Each time your code create a string literal, the JVM
checks the string literal pool first. If the string already exists in
the pool, a reference to the pooled instance returns. If the string
does not exist in the pool, a new String object instantiates, then is
placed in the pool. Java can make this optimization since strings are
immutable and can be shared without fear of data corruption.
This comment has been removed by the author.
ReplyDelete