字符串常量池
Java字符串常量池 本文讨论的是JDK8及之后的版本
内存模型的变化 jdk8之后字符串常量池从Perm区转移到堆中
intern机制的变化 如果字符串常量池中存在(StringTable在比较时equals返回true),则返回该字符串对象地址;
如果字符串常量池中不存在,则返回调用intern的字符串对象在堆中的地址,达到复用该对象
字符串常量池的添加方式 通过双引号字符串声明添加 //创建一个对象 String s = "test"; //创建hello,world两个字符串常量池中对象,两个栈中中间变量,一个堆中最终变量"helloworld",注意此字符串不在常量池中 String s = new String("hello") + new String("world"); 通过intern方法添加 //常量池不存在时 String s = new String("hello") + new String("world"); s.