JVM篇-StringTable

String的基本特性

  • String 字符串,使用一对 “” 表示
    • String s1=“AA”;//字面量定义
    • String s2=new String("AA“”);
  • String 声明为final的不可被继承
  • String 实现了Serializable接口,表示是字符串是支持序列化的,实现Comparable接口表示:String可以比较大小
  • String在JDK8及以前内部定义了final char[] value用于存储字符串数据,jdk9改为byte[]
  • String 代表不可变的是字符序列,简称不可变性
  • 通过字面量的方式(区别于new)给一个字符安川赋值,此时字符串值声明在穿常量池中。
  • 字符串常量池是不会存储相同的字符串的
    • String的StringPool是一个固定大小的HashTable默认长度1009(JDK6)JDK7是60013
    • 通过-XX:StringTableSize可是这StringTable的长度String 8以后1009是可设置的最小的值

String 内存分配

8种基本数据类型和一种比较特殊的类型String,这些类型为了他们速度更快、更节省内存,都提供了一种常量池的概念。

  • 常量池就类似一个Java系统级别提供的缓存,8种基本类型的苍凉吃都是系统协调的,String类型的常量池比较特殊,它主要的使用方法有一下两种。
    • String str=“str”;==>双引号声明出来的String对象会直接存储在常量池中(字面量)
    • 若不是双引号声明的String对象,可以使用String提供的intern()方法
  • JDK6以前字符串常量池存放在永久代。
  • JDK7 字符串常量池在Java堆内
  • JDK8元空间,字符串常量在堆

StringTable为什么要调整?

  1. permSize默认比较小
  2. 永久代垃圾回收频率低

字符串拼接操作

  1. 常量与常量的凭借结果是在常量池,原理是编译器优化
  2. 字符串中不会存在相同的常量
  3. 只要其中有一个是变量,结果就在对重,变量拼接的原理是StringBuilder
  4. 如果凭借的结果调用intern()方法,则主动将常量池中还没有的字符串对象放入池子中,并返回此对象的地址
import org.junit.Test;

/**
 * 字符串拼接操作
 */
public class StringTest5 {
    @Test
    public void test1(){
        String s1 = "a" + "b" + "c";//编译期优化:等同于"abc"
        String s2 = "abc"; //"abc"一定是放在字符串常量池中,将此地址赋给s2
        /*
         * 最终.java编译成.class,再执行.class
         * String s1 = "abc";
         * String s2 = "abc"
         */
        System.out.println(s1 == s2); //true
        System.out.println(s1.equals(s2)); //true
    }

    @Test
    public void test2(){
        String s1 = "javaEE";
        String s2 = "hadoop";

        String s3 = "javaEEhadoop";
        String s4 = "javaEE" + "hadoop";//编译期优化
        //如果拼接符号的前后出现了变量,则相当于在堆空间中new String(),具体的内容为拼接的结果:javaEEhadoop
        String s5 = s1 + "hadoop";
        String s6 = "javaEE" + s2;
        String s7 = s1 + s2;

        System.out.println(s3 == s4);//true
        System.out.println(s3 == s5);//false
        System.out.println(s3 == s6);//false
        System.out.println(s3 == s7);//false
        System.out.println(s5 == s6);//false
        System.out.println(s5 == s7);//false
        System.out.println(s6 == s7);//false
        //intern():判断字符串常量池中是否存在javaEEhadoop值,如果存在,则返回常量池中javaEEhadoop的地址;
        //如果字符串常量池中不存在javaEEhadoop,则在常量池中加载一份javaEEhadoop,并返回次对象的地址。
        String s8 = s6.intern();
        System.out.println(s3 == s8);//true
    }

    @Test
    public void test3(){
        String s1 = "a";
        String s2 = "b";
        String s3 = "ab";
        /*
        如下的s1 + s2 的执行细节:(变量s是我临时定义的)
        ① StringBuilder s = new StringBuilder();
        ② s.append("a")
        ③ s.append("b")
        ④ s.toString()  --> 约等于 new String("ab")

        补充:在jdk5.0之后使用的是StringBuilder,在jdk5.0之前使用的是StringBuffer
         */
        String s4 = s1 + s2;//
        System.out.println(s3 == s4);//false
    }
    /*
    1. 字符串拼接操作不一定使用的是StringBuilder!
       如果拼接符号左右两边都是字符串常量或常量引用,则仍然使用编译期优化,即非StringBuilder的方式。
    2. 针对于final修饰类、方法、基本数据类型、引用数据类型的量的结构时,能使用上final的时候建议使用上。
     */
    @Test
    public void test4(){
        final String s1 = "a";
        final String s2 = "b";
        String s3 = "ab";
        String s4 = s1 + s2;
        System.out.println(s3 == s4);//true
    }
    //练习:
    @Test
    public void test5(){
        String s1 = "javaEEhadoop";
        String s2 = "javaEE";
        String s3 = s2 + "hadoop";
        System.out.println(s1 == s3);//false

        final String s4 = "javaEE";//s4:常量
        String s5 = s4 + "hadoop";
        System.out.println(s1 == s5);//true

    }

    /*
    体会执行效率:通过StringBuilder的append()的方式添加字符串的效率要远高于使用String的字符串拼接方式!
    详情:① StringBuilder的append()的方式:自始至终中只创建过一个StringBuilder的对象
          使用String的字符串拼接方式:创建过多个StringBuilder和String的对象
         ② 使用String的字符串拼接方式:内存中由于创建了较多的StringBuilder和String的对象,内存占用更大;如果进行GC,需要花费额外的时间。

     改进的空间:在实际开发中,如果基本确定要前前后后添加的字符串长度不高于某个限定值highLevel的情况下,建议使用构造器实例化:
               StringBuilder s = new StringBuilder(highLevel);//new char[highLevel]
     */
    @Test
    public void test6(){

        long start = System.currentTimeMillis();

//        method1(100000);//4014
        method2(100000);//7

        long end = System.currentTimeMillis();

        System.out.println("花费的时间为:" + (end - start));
    }

    public void method1(int highLevel){
        String src = "";
        for(int i = 0;i < highLevel;i++){
            src = src + "a";//每次循环都会创建一个StringBuilder、String
        }
//        System.out.println(src);

    }

    public void method2(int highLevel){
        //只需要创建一个StringBuilder
        StringBuilder src = new StringBuilder();
        for (int i = 0; i < highLevel; i++) {
            src.append("a");
        }
//        System.out.println(src);
    }
}

拼接效率(+与append)

  1. +:每次都会创建一个对象
  2. append:自始至终只创建一个对象
    append>=+

intern()的使用

  • 不是双引号声明的String对象,可以使用String提供的intern()方法查询字符串是否存在,若不存在就会将当前字符串 放入常量池中(String myInfo=new String(“sss”).intern();)
  • 如果任意字符串上调用string.intern方法,怎返回结果所指向的那个类实例,必须直接以常量形式出现的字符串完全相同,因此,下列表达式测试必须是true((“a”+“b”+“c”).intern()==“abc”)。
  • 通俗说 Interned String就是确保字符串在内存中只有一份拷贝,这样可以节约内存空间,加快字符串操作任务执行速度,这个值会被方法字符串内部池(String Intern Pool)。

如何保证变量String对象指向字符串常量池的数据呢?

1. 方式1 String str="abc";
2. 方式2 String str=new String("abc").intern();
3. 方式3 StringBuilder sb=new StringBuilder().append("abc").intern();

问题(下面代码输出的结果是?)

new String(“ab”)会创建几个对象,为什么?

2 两个对象
一个对象是new 关键字在堆空间创建的
另外一个对象是字符串常量池中的对象,看字节码指令可知

字节码指令

在这里插入图片描述

new String(“a”)+new String(“b”)呢?

对象1:new StringBuilder()
对象2: new String(“a”)
对象3: 常量池中的"a"
对象4: new String(“b”)
对象5: 常量池中的"b"
深入剖析: StringBuilder的toString():
对象6 :new String(“ab”)
强调一下,toString()的调用,在字符串常量池中,没有生成"ab"

分析下面代码的执行结果

public class StringIntern {
    public static void main(String[] args) {

        String s1 = new String("1");//new 两个对象,s1引用地址指向堆空间
        s.intern();//1已经存在了,常量池
        String s2 = "1";//不需要new了直接拿来用 地址指向常量池
        System.out.println(s1 == s2);// JDK 6 false jdk 7 / 8false(引用地址不一样) 

        String s3 = new String("1") + new String("1");  //相当于new String("11");但这样写不会将这个对象放进常量池。
        s3.intern();// JDK6 因为是永久代所以是创建了已经新的引用指向s3,S4再去找就得到的地址不一样所以false
        		   //  JDK7 因为有了元空间,字符串常量池放在了堆空间里,因为常量池只是保存了S3其地址引用,此时s4创建一个11得到的是S3的地址(S3的地址指向11)                                     
        String s4 = "11";
        System.out.println(s3 == s4);// JDK6  false JDK7/8 true
    }


}

图解

在这里插入图片描述

答案

答案1//s1==s2的值是false 因为s1创建了两个对象(常量池一个,指向s1引用一个) s2得到的是常量池的所以不同
答案2: //JDK6因为是永久代所以是创建了已经新的引用指向s3,S4再去找就得到的地址不一样所以false
	  //JDK7因为元空间的概念,常量池迁移到了堆内存,而堆内存中字符串常量池只保存了s3的地址值,因此s4拿到的地址与s3完全一致,因此是true

总结

  • JDK1.6中,将这个字符串对象放入串池
    • 若串池有,不会放入,返回已有串池的对象地址
    • 若串池没有,会把对象复制一份,放入,返回串池中的地址
  • JDK1.7起,将这个字符串对象放入串池
    • 如果没有,则不会放入,返回当前已有对象的地址
    • 若没有,则把对象的引用地址复制一份,放入串池,返回串池引用地址
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值