使用引用调用交换两个数字 [英] swap two numbers using call by reference

查看:235
本文介绍了使用引用调用交换两个数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们可以使用传递引用或通过引用调用在Java中交换两个数字吗?
最近,当我遇到用Java交换两个数字时,我写了

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

在main方法中,我调用上面提到的方法和取两个整数 a b 然后使用第二种方法我交换两个数字,但相对于对象本身....

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?

推荐答案

是和否。 Java永远不会通过引用传递,您的方法是一种解决方法。但是你创建一个类来交换两个整数。相反,你可以创建一个int包装器并使用pass it,这样整数可以在不需要时分开:

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.

例如,如果Java是传递引用,以下代码将打印出来 x = 1

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

但据我们所知,它打印0,因为传递给栏的参数方法是原始 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.

以下C程序也是如此:

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

如果我们想要更改 x的值,我们将不得不传递其引用(地址),如下例所示,但即使在这种情况下,我们也不会传递实际引用,而是副本引用,通过解除引用,我们将能够修改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
}

所以通过变量的地址而不是变量本身解决了C中的问题。但是在Java中,由于我们没有地址,我们需要在我们想要赋值时包装变量。在仅修改对象的情况下,我们没有那个问题,但是,如果我们想要分配它,我们必须包装它,如第一个例子中那样。这不仅适用于原始,也适用于对象,包括我刚才提到的那些包装器对象。我将在一个(更长的)示例中显示它:

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)

这篇关于使用引用调用交换两个数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆