Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><code>null</code> assignments do not change <em>value</em> by globally destroying that object. That kind of behavior would lead to hard-to-track bugs and counterintuitive behavior. They only break that specific <em>reference</em>.</p> <p>For simplicity, let's say that <code>sample</code> points to address 12345. This is probably not the address, and is only used to make things simple here. <strike>The address is <em>typically</em> represented with the weird hexadecimal given in <code>Object#hashCode()</code>, but this is implementation-dependent.</strike><sup>1</sup></p> <pre><code>StringBuilder sample = new StringBuilder(); //sample refers to //StringBuilder at 12345 StringBuilder referToSample = sample; //referToSample refers to //the same StringBuilder at 12345 //SEE DIAGRAM 1 referToSample = null; //referToSample NOW refers to 00000, //so accessing it will throw a NPE. //The other reference is not affected. //SEE DIAGRAM 2 sample.append("A"); //sample STILL refers to the same StringBuilder at 12345 System.out.println(sample); </code></pre> <p><em>From the lines marked <code>See diagram</code> the diagrams of the objects at that time are as follows:</em></p> <p><strong>Diagram 1:</strong></p> <pre><code>[StringBuilder sample] -----------------&gt; [java.lang.StringBuilder@00012345] ↑ [StringBuilder referToSample] ------------------------/ </code></pre> <p><strong>Diagram 2:</strong></p> <pre><code>[StringBuilder sample] -----------------&gt; [java.lang.StringBuilder@00012345] [StringBuilder referToSample] ----&gt;&gt; [null pointer] </code></pre> <p>Diagram 2 shows that annulling <code>referToSample</code> does not break the reference of <code>sample</code> to the StringBuilder at <code>00012345</code>.</p> <p><sub><sup>1</sup>GC considerations make this implausible.</sub></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.
 

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