可能重复:
Java是否通过引用传递?
因此,请考虑以下两个示例及其各自的输出:
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;
}
}
Run Code Online (Sandbox Code Playgroud)
输出:
1
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");
}
}
Run Code Online (Sandbox Code Playgroud)
输出:
www.yahoo.com
为什么会这样www.yahoo.com?对我来说看起来不像passbyvalue.
| 归档时间: |
|
| 查看次数: |
284 次 |
| 最近记录: |