Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Yes and no. Java never passes by reference, and your way is one workaround. But yet you create a class just to swap two integers. Instead, you can create an int wrapper and use pass it, this way the integer may be separated when not needed:</p> <pre><code>public class IntWrapper { public int value; } // Somewhere else public void swap(IntWrapper a, IntWrapper b) { int temp = a.value; a.value = b.value; b.value = temp; } </code></pre> <hr> <p>As the comments show, I might not have been clear enough, so let me elaborate a little bit.</p> <p>What does passing by reference mean? It means that when you pass an argument to the method, you can change the original argument itself inside this method.</p> <p>For example, if Java was pass-by-reference, the following code will print out <code>x = 1</code>:</p> <pre><code>public class Example { private static void bar(int y) { y = 10; } public static void main(String[] args) { int x = 1; bar(x); System.out.println("x = " + x); } } </code></pre> <p>But as we know, it prints 0, since the argument passed to the <code>bar</code> method <strong>is a copy</strong> of the original <code>x</code>, and any assignment to it will not affect <code>x</code>.</p> <p>The same goes with the following C program:</p> <pre><code>static void bar(int y) { y = 1; } int main(int argc, char * argc[]) { int x = 0; bar(x); printf("x = %d\n", x); } </code></pre> <p>If we want to change the value of <code>x</code>, we will have to pass its reference (address), as in the following example, but even in this case, we will not pass the actual reference, but <strong>a copy</strong> of the reference, and by dereferencing it we will be able to modify the actual value of x. Yet, direct assignment to the reference will no change the reference itself, as it is passed by value:</p> <pre><code>static void bar(int &amp;y) { *y = 1; y = NULL; } int main(int argc, char * argc[]) { int x = 0; int * px = &amp;x; bar(px); printf("x = %d\n", x); // now it will print 1 printf("px = %p\n", px); // this will still print the original address of x, not 0 } </code></pre> <p>So passing the address of the variable instead of the variable itself solves the problem in C. But in Java, since we don't have addresses, we need to wrap the variable when we want to assign to it. In case of only modifying the object, we don't have that problem, but again, if we want to assign to it, we have to wrap it, as in the first example. This apply not only for primitive, but also for objects, including those wrapper objects I've just mentioned. I will show it in one (longer) example:</p> <pre><code>public class Wrapper { int value; private static changeValue(Wrapper w) { w.value = 1; } private static assignWrapper(Wrapper w) { w = new Wrapper(); w.value = 2; } public static void main(String[] args) { Wrapper wrapper = new Wrapper(); wrapper.value = 0; changeValue(wrapper); System.out.println("wrapper.value = " + wrapper.value); // will print wrapper.value = 1 assignWrapper(w); System.out.println("wrapper.value = " + wrapper.value); // will still print wrapper.value = 1 } } </code></pre> <p>Well, that's it, I hope I made it clear (and didn't make too much mistakes)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload