Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You are correct. It is definitely worth being <em>aware</em> of this issue, even if it doesn't affect you every time.</p> <p>As you say, Strings cannot change after creation - they're immutable and they don't expose many ways to change them.</p> <p>However, operations such as a split() will be generating additional string objects in the background, and each of those strings have a <a href="http://www.javamex.com/tutorials/memory/string_memory_usage.shtml" rel="nofollow noreferrer">memory overhead</a> <strong>if you are holding onto references to them</strong>.</p> <p>As the other posters note, the objects will be small and garbage collection will usually clean up the old ones after they have gone out of scope, so you generally won't have to worry about this.</p> <p>However, if you're doing something specific and holding onto large amounts of string references then this could bite you. </p> <p>Look at String <a href="http://www.codeinstructions.com/2009/01/busting-javalangstringintern-myths.html" rel="nofollow noreferrer">interning</a> depending on your use case, noting the warnings on the linked page. </p> <p>Two things to note:</p> <p>1) Hard coded String literals will be <a href="https://stackoverflow.com/questions/1855170/when-should-we-use-intern-method-of-string">automatically interned</a> by Java, reducing the impact of this. </p> <p>2) The + operator is more efficient in this regard, it will use String Builders underneath giving performance &amp; memory benefits.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload