String s="hello";s+="world";s变化了吗?原始的String对象的内容变了吗?

分析:

String s="hello";s+="world";

引用变量s 一开始指向String对象("hello" :0x001);

                                                  ("world":0x002);

s拼接后,就重新指向String对象("helloworld":0x003);

答:s改变了,原始的String对象 "hello" 内容并没有改变,仍然存在于内存中;

   因为String是final修饰,是最终类,不能被继承,即String类的对象也是不能改变,所以原始的对象内容不改变

 注意:是对象不能改变而不是引用变量不能改变

原文地址:https://www.cnblogs.com/yuefeng123/p/7351879.html