title: Passing objects to methods java
body: <p>I'm using three objects: <code>StringBuilder</code>, <code>Integer</code>, and <code>testobject</code> - that are passed to a method to change its state. </p>
  
  <p>As expected, the <code>StringBuilder</code> and <code>testobject</code> points to the same object and the state is change but it does not work for <code>Integer</code> object.</p>
  
  <pre><code>class testobject{
      int x = 1;
  }
  
  public class test{
      public static void main(String[] args){
          StringBuilder s1 = new StringBuilder("String");
          go(s1);
          System.out.println(s1);
  
          Integer s2 = new Integer("20");
          go1(s2);
          System.out.println(s2);
  
          testobject s3 = new testobject();
          go2(s3);
          System.out.println(s3.x);
      }
  
      static void go(StringBuilder s1){
          s1.append("Builder");
      }
  
      static void go1(Integer s2){
          s2 = 1;
      }
      static void go2(testobject s3){
          s3.x = 5;
      }
  </code></pre>
  
  <p>Result:</p>
  
  <pre><code>StringBuilder
  20
  5
  </code></pre>
  
  <p>ExpectedResult:</p>
  
  <pre><code>StringBuilder
  1
  5
  </code></pre>