public class Test {
String str = new String("hello");
char[] ch = {'a','b','c'};
public void function(String str,char ch[]){
str = "world";
ch[1] = 'd';
}
public static void main(String[] args) {
Test test1 = new Test();
test1.function(test1.str,test1.ch);
System.out.println(test1.str+ " and ");
System.out.println(test1.ch);
}
}
我们来看下上面代码,定义一个String对象,并赋初始值为“hello”, 定义一个ch字符数组,定义一个function方法,里面有两参数,并赋初始值,world和字符索引为1的字符'd',当我们来测试的时候,创建Test对象,调用function函数,str的值world被覆盖变成hello,ch的值第索引为1的值替换成了adc,所以输出为 hello and adc