Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Interning is something that happens behind the scenes, so you as a programmer never have to worry about it. You generally do not have to put anything to the pool, or get anything from the pool. Like garbage collection: you never have to invoke it, or worry that it may happen, or worry that it may not happen. (Well, in 99.999% of the cases. And the remaining 0.001 percent is when you are doing very weird stuff.)</p> <p>The compiler takes care of interning all string literals that are contained within your source file, so "lalala" will be interned without you having to do anything, or having any control over the matter. And whenever you refer to "lalala" in your program, the compiler makes sure to fetch it from the intern pool, again without you having to do anything, nor having any control over the matter.</p> <p>The intern pool contains a more-or-less fixed number of strings, generally of a very small size, (only a fraction of the total size of your .exe,) so it does not matter that they never get garbage-collected.</p> <hr> <p><strong>EDIT</strong> </p> <p>The purpose of interning strings is to greatly improve the execution time of certain string operations like Equals(). The <code>Equals()</code> method of <code>String</code> first checks whether the strings are equal by reference, which is extremely fast; if the references are equal, then it returns <code>true</code> immediately; if the references are not equal, and the strings are both interned, then it returns <code>false</code> immediately, because they cannot possibly be equal, since all strings in the intern pool are different from each other. If none of the above holds true, then it proceeds with a character by character string comparison. (Actually, it is even more complicated than that, because it also checks the hashcodes of the strings, but let's keep things simple in this discussion.)</p> <p>So, suppose that you are reading tokens from a file in string <code>s</code>, and you have a switch statement of the following form:</p> <pre><code>switch( s ) { case "cat": .... case "dog": .... case "tod": .... } </code></pre> <p>The string literals "cat", "dog", "tod" have all been interned, but you are comparing each and every one of them against <code>s</code>, which has not been interned, so you are not reaping the benefits of the intern pool. If you intern <code>s</code> right before the switch statement, then the comparisons that will be done by the switch statement will be a lot faster.</p> <p>Of course, if there is any possibility that your file might contain garbage, then you do NOT want to do this, because loading lots of random strings into the intern pool is sure to kill the performance of your program, and eventually run out of memory.</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. 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.
 

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