swap two numbers using call by reference

2019-05-26 07:30发布

问题:

Can we swap two numbers in Java using pass by reference or call by reference? Recently when I came across swapping two numbers in Java I wrote

class Swap{
    int a,b;
    void getNos(){
        System.out.println("input nos");
        a = scan.nextInt();
        b = scan.nextInt(); // where scan is object of scanner class
    }
    void swap(){
        int temp;
        temp = this.a;
        this.a = thisb;
        this.b = this.a;
    }
}

In the main method I call the above mentioned methods and take two integers a,b and then using the second method I swap the two numbers, but relative to the object itself....

Does this program or logic come under pass by reference? And is this correct solution?

回答1:

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:

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;
}

As the comments show, I might not have been clear enough, so let me elaborate a little bit.

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.

For example, if Java was pass-by-reference, the following code will print out x = 1:

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);
    }
}

But as we know, it prints 0, since the argument passed to the bar method is a copy of the original x, and any assignment to it will not affect x.

The same goes with the following C program:

static void bar(int y) {
    y = 1;
}
int main(int argc, char * argc[]) {
    int x = 0;
    bar(x);
    printf("x = %d\n", x);
}

If we want to change the value of x, 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 a copy 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:

static void bar(int &y) {
    *y = 1;
    y = NULL;
}
int main(int argc, char * argc[]) {
    int x = 0;
    int * px = &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
}

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:

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
    }
}

Well, that's it, I hope I made it clear (and didn't make too much mistakes)