本人刚入门不久,最近在开发过程中发现了个问题不是很明白,
public class Test {
public static void main(String args[]){
String str = "String";
int [] arr = {1,2};
Test2 t2 = new Test2();
t2.fun(str);
t2.fun2(arr);
System.out.println(str);
System.out.println(arr[0]);
System.out.println(arr[1]);
}
}
class Test2 {
public void fun(String str){
str = "3333333";
}
public void fun2(int[] arr){
arr[0]=100;
arr[1]=200;
}
}
输出结果:
String
100
200
为什么只是调用了test2方法确使得数组的值发生了变化,String却没有变呢?
public class Test {
public static void main(String args[]){
String str = "String";
int [] arr = {1,2};
Test2 t2 = new Test2();
t2.fun(str);
t2.fun2(arr);
System.out.println(str);
System.out.println(arr[0]);
System.out.println(arr[1]);
}
}
class Test2 {
public void fun(String str){
str = "3333333";
}
public void fun2(int[] arr){
arr[0]=100;
arr[1]=200;
}
}
输出结果:
String
100
200
为什么只是调用了test2方法确使得数组的值发生了变化,String却没有变呢?