JAVA的参数传递

来源:百度知道 编辑:UC知道 时间:2024/05/21 19:38:38
public class TestPsss
{
public void change(int a)
{
a=100;
}
public static void main(String[] args)
{
int pass=50;
TestPsss p=new TestPsss();
System.out.println(pass);
p.change(pass);
System.out.println(pass);

}

}

为什么答案是 50 50.。。麻烦详细的解释下 谢谢

change(int a)
p.change(pass);
虽然你吧PASS当作参数传递进去了,但是方法在执行上实际在内存中新建立一个对象a 并且a=100
并且你的程序中没有p=a或者p=100
所以P的内容实际上还是50

因为你change里面没有要把100传递给pass得方法阿
想得到 50 100得结果 下面这段代码 就可以了
public class TestPsss
{
static int pass = 50;
public void change(int a)
{

a=100;
pass=a;
}
public static void main(String[] args)
{

TestPsss p=new TestPsss();
System.out.println(pass);
p.change(pass);
System.out.println(pass);

}
}

change(int a) 与 pass 是2个不同的栈