JAVA pass-by-value Vs pass-by-reference
<blockquote>
  <p><strong>Possible Duplicate:</strong><br>
  <a href="http://stackoverflow.com/questions/40480/is-java-pass-by-reference">Is Java “pass-by-reference”?</a>  </p>
</blockquote>



<p>I am trying to understand the difference between the 2 JAVA programs written below:</p>

<pre><code> public class Swapping {

public static void main(String[] args) {

    IntWrap i = new IntWrap(10);
    IntWrap j = new IntWrap(20);

    swap(i, j);
    System.out.println("i " + i.i + ", j " + j.i);
}

public static void swap(IntWrap i , IntWrap j){

    int x = i.i;
    i.i = j.i;
    j.i = x;

    System.out.println("i " + i.i + ", j " + j.i);
}

}

public class IntWrap {

int i;
public IntWrap(int i){
    this.i = i;
}

}
</code></pre>

<p>Output : </p>

<pre><code>i 20, j 10
i 20, j 10
</code></pre>

<p><strong>And 2nd one: -</strong></p>

<p>public class Swapping {</p>

<pre><code>public static void main(String[] args) {

    Integer i = new Integer(10);
    Integer j = new Integer(20);

    swap(i, j);
    System.out.println("i " + i + ", j " + j);
}

public static void swap(Integer i , Integer j){

    Integer temp = new Integer(i);
    i = j;
    j = temp;

    System.out.println("i " + i + ", j " + j);
}

}
</code></pre>

<p>Output : </p>

<pre><code>i 20, j 10
i 10, j 20
</code></pre>

<p>I am not able to understand that I even though I am passing the Integer object, it should get swapped in the original program. What difference did it created if I wrote the wrapper class on the top of it as I am again passing the object only....Please respond with detailed explanation only, if you have any.</p>
