public class Test04 {
public void change(String str, char ch[],Integer a)
{
str = "test ok";
ch[0] = 'g';
a=20;
}
@Test
public void test01()
{
String str = new String("good");
char[] ch = { 'a', 'b', 'c' };
Integer a=new Integer(10);
Test04 ex = new Test04();
ex.change(str, ch,a);
System.out.print(str + "and ");
System.out.print(ch);
System.out.println();
System.out.println(a);
}
}
测试结果:
goodand gbc
10;
不可变类(Immutable class)是指当创建了这个类的实例后,就不允许修改它的值了,
也就是说,一个对象一旦被创建出来,在其整个生命周期中,它的成员变量就不能被修改了。
它有点类似于常量(const),即只允许其他程序进行读操作,而不允许其他程序进行修改操
作。
在 Java 类库中,所有基本类型的包装类都是不可变类,例如 Integer、 Float 等。此外,
String 也是不可变类。