title: How can I pass an Integer class correctly by reference?
body: <p>I am hoping that someone can clarify what is happening here for me.  I dug around in the integer class for a bit but because integer is <em>overriding</em> the <code>+</code> operator I could not figure out what was going wrong.  My problem is with this line:</p>
  
  <pre><code>Integer i = 0;
  i = i + 1;  // ← I think that this is somehow creating a new object!
  </code></pre>
  
  <p>Here is my reasoning: 
  I know that java is pass by value (<a href="https://stackoverflow.com/questions/40480?tab=votes#tab-top">or pass by value of reference</a>), so I think that in the following example the integer object should be incremented each time.  </p>
  
  <pre><code>public class PassByReference {
  
      public static Integer inc(Integer i) {
          i = i+1;    // I think that this must be **sneakally** creating a new integer...  
          System.out.println("Inc: "+i);
          return i;
      }
  
      public static void main(String[] args) {
          Integer integer = new Integer(0);
          for (int i =0; i&lt;10; i++){
              inc(integer);
              System.out.println("main: "+integer);
          }
      }
  }
  </code></pre>
  
  <p>This is my expected output:</p>
  
  <pre>
  Inc: 1
  main: 1
  Inc: 2
  main: 2
  Inc: 3
  main: 3
  Inc: 4
  main: 4
  Inc: 5
  main: 5
  Inc: 6
  main: 6
  ...
  </pre>
  
  <p>This is the actual output.  </p>
  
  <pre>
  Inc: 1
  main: 0
  Inc: 1
  main: 0
  Inc: 1
  main: 0
  ...
  </pre>
  
  <p>Why is it behaving like this?  </p>