所以考慮以下兩個例子,它們各自的輸出:
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