String 类是不可改变的解析,例如:

String s = "Google";
System.out.println("s = " + s);

s = "Runoob";
System.out.println("s = " + s);

输出结果为:

Google
Runoob

从结果上看是改变了,但为什么门说String对象是不可变的呢?

原因在于实例中的 s 只是一个 String 对象的引用,并不是对象本身,当执行 s = “Runoob”; 创建了一个新的对象 “Runoob”,而原来的 “Google” 还存在于内存中。

posted on 2017-04-12 16:52  为了八千块  阅读(116)  评论(0编辑  收藏  举报