JAVA中参数传递问题,为什么ab的值不被改变, 怎样才能被改变!

public class MyTest {
public static int c =1;
public static int d = 2;
public static void main(String[] args) {

    System.out.print("交换前\ta=" + c + "\tb=" + d + "\r");
    Swap(c, d);
    System.out.print("交换后\ta=" + c + "\tb=" + d + "\r");
}

// 交换a,b两个变量的值
public static void Swap(int c, int d) {
    int temp = c;
    c = d;
    d = temp;
    System.out.print("方法内\ta=" + c + "\tb=" + d + "\r");

}

}
结果:
交换前 a=1 b=2
方法内 a=2 b=1
交换后 a=1 b=2

为什么ab的值不变,怎么才能改变ab呢?不是标记为静态存储了吗

改为object或者利用数组
参考:
http://blog.csdn.net/jianzhizg/article/details/1427086

希望采纳我的答案哦 ^_^

java中基本类型没有指针和引用的概念,肯定没有C和C++或者C#之类的方便,但可以借助面向对象的方式,封装一个类。
类似


/**
 * A instance of this class will have a ability to swap two integers.

 */
class Swaper
{
    int x, y;

    public Swaper(int x, int y)
    {
        super();
        this.x = x;
        this.y = y;
    }

    // The swap behavior is belong to a Swaper object.
    public Swaper swap()
    {
        return new Swaper(this.y, this.x);
    }
}

public class JavaDemo
{
    public static void main(String[] args)
    {
        int x = 3;
        int y = 4;

        System.out.println("x: " + x + ", y: " + y);

        Swaper swaper = new Swaper(x, y);

        x = swaper.swap().x;
        y = swaper.swap().y;

        System.out.println("x: " + x + ", y: " + y);
    }
}

为什么值没有变,原因有以下几点:
1 值传递;
2 就近原则。

修改方式:
1:如上;
2. 要实现效果,只需在原基础上去掉swap中的参数就可以,因为您的c d.已是静态,去掉参数后,用的就是外面的cd

你缺少的只是 值传递 和 引用传递的概念

原因是非引用传递,这个自行补脑java基础吧

你用的是 方法(参数)这样的方式来做,尤其你的参数还是int 这种好拷贝的参数。造成的结果就是在内存中新开创了一个拷贝值然后拿去交换,原值是不变的。
Java对象是引用传参,数组是地址传参,所以改了会被承认。但是拷贝传参不会被承认的。既然是静态的变量,直接用this加点的方式直接在方法里给改了不就行了嘛