源自一个面试的问题:字符串在函数的参数中是当成值传递还是引用传递。
这是测试方法:
public void setA(String a)
{
System.out.println("In the setmethod before set:#### " + a);
a = "I am a student";
System.out.println("In the setmethod after set:#### " + a);
}
public static void main(String[] args)
{
String a = "China is our motherland";
System.out.println("Out before set:----- "+ a);
new UserQuery().setA(a);
System.out.println("Out after set:----- "+a);
}
结果显示
Out before set:----- China is our motherland
In the setmethod before set:#### China is our motherland
In the setmethod after set:#### I am a student
Out after set:----- China is our motherland
这样的话,如果要在一个方法里面修改传入的参数的值,岂不是不行?如果这样的该怎么办呢?