所以考虑以下两个例子,它们各自的输出:
public class LooksLikePassByValue {
public static void main(String[] args) {
Integer num = 1;
change(num);
System.out.println(num);
}
public static void change(Integer num)
{
num = 2;
}
}
输出:
public class LooksLikePassByReference {
public static void main(String[] args) {
Properties properties = new Properties();
properties.setProperty("url", "www.google.com");
change(properties);
System.out.println(properties.getProperty("url"));
}
public static void change(Properties properties2)
{
properties2.setProperty("url", "www.yahoo.com");
}
}
输出:
www.yahoo.com
为什么会变成这样www.yahoo.com?
这看起来并不像passbyvalue我。
另请参阅:http://stackoverflow.com/questions/40480/is-java-pass-by-reference – 2011-08-08 18:57:41