public class TiaoShi {
public static void main(String[] args) {
String s = “hello”;
s += “world”;
System.out.println(s);
}
}
分析:1、字符串直接赋值的方式是在方法区里找,在字符串常量池中找,
如果有就直接返回,没有就进去,创建并返回给一个地址
String s 指向字符串常量池里的”hello” 给一个地址只0x001
2、”world”就进字符串常量池,给一个地址0x002
3、s += “world”;
再造了一个空间,把 “hello”“world”拼起来,
有一个地址值oxoo3 把0x003赋值给了s
“hello”不指向s变成”helloworld”指向s
字符串的特点一旦被赋值就不能被改变, 值不能变,不是引用不能变,