Abu*_*mad 0 java string pass-by-reference
在以下代码中
public class Test {
public static void main(String[] args){
int [] arr = new int[]{1,2};
String b=new String("abc");
f(b,arr);
System.out.println(b);
System.out.println(arr[0]);
}
public static void f(String b, int[] arr){
b+="de";
b=null;
arr[0] = 5;
}
}
Run Code Online (Sandbox Code Playgroud)
为什么字符串的引用变量不像数组的引用变量?
我知道字符串是不可变的,因此对它们的操作会创建新的字符串,但是如何引用字符串以及引用b如何仍然引用旧值,尽管它被更改为引用f()方法中的其他内容.
| 归档时间: |
|
| 查看次数: |
339 次 |
| 最近记录: |