The results indicate the following programs running:
public class Example{
String str=new String("tarena");
char[]ch={'a','b','c'};
public static void main(String args[]){
Example ex=new Example();
ex.change(ex.str,ex.ch);
System.out.print(ex.str+" and ");
System.out.print(ex.ch);
}
public void change(String str,char ch[]){
//引用类型变量,传递的是地址,属于引用传递。
str="test ok";
ch[0]='g';
}
}
A.tarena and abc
B.tarena and gbc
C.test ok and abc
D.test ok and gbc
This title selected B.
string and char array is a reference type, a reference type is passed address will affect the value of the original variable, but string is a special reference type, why? Because the value of type string are immutable, in order to consider some memory, integrated security reasons, set it to immutable; immutable is how to achieve? Java in memory specifically for the string opened a string constant pool, used to lock the data is not tampered with, so the title str variable in the function and the original str is not a thing, it is a local reference, point to a testok string, with the end of the function, it is also nothing, but a char array will change the original value