title: Confusion over Java's pass-by-value and immutability
body: <p>In preparation for the SCJP (or OCPJP as it's now known) exam, I'm being caught out by some mock questions regarding pass-by-(reference)value and immutability.</p>
  
  <p>My understanding, is that when you pass a variable into a method, you pass a copy of the bits that represent how to get to that variable, not the actual object itself.</p>
  
  <p>The copy that you send in, points to the same object, so you can modify that object if its mutable, such as appending to a StringBuilder. However, if you do something to an immutable object, such as incrementing an Integer, the local reference variable now points to a new object, and the original reference variable remains oblivious to this.</p>
  
  <p>Consider my example here :</p>
  
  <pre><code>public class PassByValueExperiment
  {
  
      public static void main(String[] args)
      {
          StringBuilder sb = new StringBuilder();
          sb.append("hello");
          doSomething(sb);
          System.out.println(sb);
  
  
          Integer i = 0;
          System.out.println("i before method call : " + i);
          doSomethingAgain(i);
          System.out.println("i after method call: " + i);
      }
  
      private static void doSomethingAgain(Integer localI)
      {
          // Integer is immutable, so by incrementing it, localI refers to newly created object, not the existing one
          localI++;
      }
  
      private static void doSomething(StringBuilder localSb)
      {
          // localSb is a different reference variable, but points to the same object on heap
          localSb.append(" world");
      }
  }
  </code></pre>
  
  <p><strong>Question</strong> : Is it only immutable objects that behave in such a manner, and mutable objects can be modified by pass-by-value references? Is my understanding correct or are there other perks in this behaviour?</p>