title: Number of objects created
body: <p>Today, I had the following question in exam:</p>
  
  <pre><code>String[] subjects=new String[10];
  String subname="POPL-201617";
  subjects[0]=subName;
  subName=null;
  </code></pre>
  
  <ol>
  <li><p>How many objects are created?</p>
  
  <p>My answer: 11 objects will be created. 10 from the first statement and one is the string literal.</p></li>
  <li><p>How many references to those objects exist after the code executes?</p>
  
  <p>My answer: There is one reference to "POPL-201617" in total.</p></li>
  <li><p>Explain whether these objects are eligible for garbage collection.</p>
  
  <p>My answer: Since there is a reference to "POPL-201617", it is not eligible for garbage collection. Even if there was no reference, it is an interned string and hence, can't be garbage collected. Rest all are eligible for garbage collection.</p></li>
  </ol>
  
  <p>I am concerned about the first statement. We're not passing any argument to the constructor of the String class. Will it form the objects or just space is allocated on the heap?</p>
  
  <p>Also, my understanding says that subjects initially points to the base of the array. So, the first element initially had a reference. But since subjects[0] now refers to POPL, the no array element has any reference.</p>
  
  <p>Am I correct? Or am I missing something?</p>