用程序理解java中的swap函数

用程序理解java中的swap函数,java,Java,正如我在网上读到的,Java是按值传递的,一般的交换函数不会交换这两个值。我还读到不可能交换基元类型的值。我想知道为什么下面的程序在交换后工作并显示不同的valies public class swapMe { public static void main(String[] args) { int x = 10 , y = 20; System.out.println("Before"); System.out.println("Fi

正如我在网上读到的,Java是按值传递的,一般的交换函数不会交换这两个值。我还读到不可能交换基元类型的值。我想知道为什么下面的程序在交换后工作并显示不同的valies

public class swapMe {

    public static void main(String[] args) {
        int x = 10 , y = 20;

        System.out.println("Before");
        System.out.println("First number = " + x);
        System.out.println("Second number = " + y);

        int temp = x;
        x = y;
        y = temp;

        System.out.println("After");
        System.out.println("First number = " + x);
        System.out.println("Second number = " + y);
    }
}
它是否像某个地方一样,
x=10
y=20
的原始值仍然存储在某个地方,并且显示的交换值不正确?请告知。谢谢

我不完全确定你从哪里得到这些信息,但让我们一次看一次

正如我在网上读到的,Java是按值传递的,一般的交换函数不会交换这两个值

正确…如果期望通过调用方法进行交换

 public void swap(int x, int y) {
     int tmp = x;
     x = y;
     y = tmp;
 }

 // meanwhile, in main
 int x = 10;
 int y = 20;
 swap(x, y);
 System.out.println(x); // still prints 10
 System.out.println(y); // still prints 20
不正确…如果交换发生在方法内部并且以某种方式使用

 public void swap(int x, int y) {
     int tmp = x;
     x = y;
     y = tmp;
     System.out.println(x);  // will print 20 from main
     System.out.println(y);  // will print 10 from main
 }

 // meanwhile, in main
 int x = 10;
 int y = 20;
 swap(x, y);
 System.out.println(x); // still prints 10
 System.out.println(y); // still prints 20
我还读到不可能交换基元类型的值

不,这是完全可能做到的。您始终可以重新分配变量

至于上面的例子为什么有效,这是因为您在重新分配其中一个变量的同时保留了其中一个值。你基本上是把一个值放在一边,然后把它复制过来

慢慢地

int x = 10;
int y = 20;
int tmp = x; // tmp = 10
x = y;  // x = 20, tmp = 10
y = tmp;  x = 20, y = 10; tmp = 10 (but that doesn't matter)
不完全确定你从哪里得到这些信息,但让我们一次拿一个

正如我在网上读到的,Java是按值传递的,一般的交换函数不会交换这两个值

正确…如果期望通过调用方法进行交换

 public void swap(int x, int y) {
     int tmp = x;
     x = y;
     y = tmp;
 }

 // meanwhile, in main
 int x = 10;
 int y = 20;
 swap(x, y);
 System.out.println(x); // still prints 10
 System.out.println(y); // still prints 20
不正确…如果交换发生在方法内部并且以某种方式使用

 public void swap(int x, int y) {
     int tmp = x;
     x = y;
     y = tmp;
     System.out.println(x);  // will print 20 from main
     System.out.println(y);  // will print 10 from main
 }

 // meanwhile, in main
 int x = 10;
 int y = 20;
 swap(x, y);
 System.out.println(x); // still prints 10
 System.out.println(y); // still prints 20
我还读到不可能交换基元类型的值

不,这是完全可能做到的。您始终可以重新分配变量

至于上面的例子为什么有效,这是因为您在重新分配其中一个变量的同时保留了其中一个值。你基本上是把一个值放在一边,然后把它复制过来

慢慢地

int x = 10;
int y = 20;
int tmp = x; // tmp = 10
x = y;  // x = 20, tmp = 10
y = tmp;  x = 20, y = 10; tmp = 10 (but that doesn't matter)

(facepalm!)您是否将这些值传递给任何方法,以检查交换函数不会交换理论???存储在
temp
int temp=x之后的
x
执行。等等它们不在“某处”,您自己告诉编译器它们应该在哪里。您需要理解变量之间的文字交换和交换+赋值之间的区别。另外——“正如我在网上读到的,Java是按值传递的”在你的示例中,你从未将任何变量传递给任何方法(facepalm!)你是否将这些值传递给任何方法,以检查交换函数不会交换理论???存储在
temp
int temp=x之后的
x
执行。等等它们不在“某处”,您自己告诉编译器它们应该在哪里。您需要理解变量之间的文字交换和交换+赋值之间的区别。此外,“正如我在网上读到的,Java是按值传递的”在您的示例中,您从未将任何变量传递给任何方法。从技术上讲,您不能在原语之间“交换”值:
4
将保留
4
,无论您如何使用它。OP只是混淆了交换和赋值。@M.Prokhorov:这不像Python的交换风格,不需要中间变量,但在这种情况下,重新分配与交换是一致的。我很确定Python也使用第三个变量,它只是不会向您显示。@M.Prokhorov:我说过您不需要它。Python可能需要它,但您不需要调用它
x,y=y,x
x
y
的定义值的最常见形式。从技术上讲,您不能在原语之间“交换”值:
4
将保留
4
。OP只是混淆了交换和赋值。@M.Prokhorov:这不像Python的交换风格,不需要中间变量,但在这种情况下,重新分配与交换是一致的。我很确定Python也使用第三个变量,它只是不会向您显示。@M.Prokhorov:我说过您不需要它。Python可能需要它,但您不需要调用它
x,y=y,x
是定义值
x
y
的最常见形式。