title: How many String objects will be created
body: <p>I have the following Java code:</p>
  
  <pre><code>public String makinStrings() {
    String s = "Fred";
    s = s + "47";
    s = s.substring(2, 5);
    s = s.toUpperCase();
    return s.toString();
  }
  </code></pre>
  
  <p>The question is somehow simple: how many <code>String</code> objects will be created when this method is invoked?</p>
  
  <p>At the beginning I answered that 5 String objects are created, but the answer from my book says that only 3 objects are created and no explanation was given (this is a SCJP question).</p>
  
  <p>From my point of view there are 5 objects:
  "Fred", "47", "Fred47", "ed4", "ED4".</p>
  
  <p>I also found this question on a SCJP simulation exam, with the same answer 3.</p>