Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java中的简单交换_Java_Swap - Fatal编程技术网

Java中的简单交换

Java中的简单交换,java,swap,Java,Swap,在我看来,它们看起来是一样的,但为什么它们产生不同的输出?我是Java新手,所以请容忍我 这个交换功能可以工作 //Swap 1 Output is "4,8" public class SampleSwap { public static void main(String[] args) { int a=8; int b=4; int temp; temp=a; a=b; b=temp; System.out.println("a

在我看来,它们看起来是一样的,但为什么它们产生不同的输出?我是Java新手,所以请容忍我

这个交换功能可以工作

//Swap 1 Output is "4,8"
public class SampleSwap {
public static void main(String[] args)
{   
    int a=8;
    int b=4;
    int temp;

    temp=a;
    a=b;
    b=temp;

   System.out.println("a:" +a);
   System.out.println("b:" +b);
}
}
此交换功能不起作用

//Swap 2 Output is "8,4" 
public class Swap {
public static void main(String[] args) {
    int a = 8, b = 4;
    swap(a, b);
        System.out.print(a + "," + b);
    System.out.println();
}

public static void swap(int a, int b) {
    int tmp = a;
    a = b;
    b = tmp;
}
}

这些参数是按值传递的。他们不会改变原件