将字符串 a 传递给 myFunction()时,我会传递引用。为什么退出时,引用指向旧String?它不使用字符串的实际引用吗?
import java.time.*;
public class Main {
public static void main(String[] args) {
String a = "aaa";
myFunction(a);
System.out.println(a);
}
private static void myFunction(String a) {
a = a + "111";
System.out.println(a);
}
}
答案 0 :(得分:0)
Look into variable scope. You are creating a local copy of the a variable and setting it in the myFunction() . The local copy only lives as long as the method does. When the Myfunction() completes its modified copy of the "a" is no longer reachable.