文件名称:不变模式-Chap6 Java常用类
文件大小:330KB
文件格式:PPT
更新时间:2024-05-16 03:14:43
java常用类
不变模式 一个字符串对象被创建之后,它的值就不可改变,直到它被当做垃圾回收。 public class StringTest { public static void main(String[] args) { String str1="hello"; System.out.println("str1="+str1); str1=str1+",world!"; System.out.println("str1="+str1); } } 运行结果: str1=hello str1=hello,world! hello str1 hello,world! 当执行字符串连接运算时,系统实际上在串池中创建了一个新的字符串对象“hello,world!”,对象str1指向它。 字符串“hello”始终不变,当没有对象指向它时,变成垃圾。 串池