Java中的以下代码有什么区别?我无法清楚地了解对象是如何在 Java 中传递的。谁能解释下面的代码。
package cc;
public class C {
public static class Value {
private String value;
public void setValue(String str) {
value=str;
}
public String getValue() {
return value;
}
}
public static void test(Value str) {
str.setValue("test");
}
public static void test2(Value str) {
str=new Value();
str.setValue("test2");
}
public static void main(String[] args) {
String m="main";
Value v=new Value();
v.setValue(m);
System.out.println(v.getValue()); // prints main fine
test(v);
System.out.println(v.getValue()); // prints 'test' fine
test2(v);
System.out.println(v.getValue()); // prints 'test' again after assigning to test2 in function why?
}
}