String&StringBuilder&StringBuffer总结

一、String的JVM内存分配测试与分析 
        
        String a="a";
        String b="b";
        String c="ab";
        String d="ab";
        String e=a+b;
        String f = "a" + "b";
        final String p = "a";
        final String q = "b";
        String m = p + q;
        String n = p + "b";
        String str1 = new String("ab");
        String str2 = new String("ab");
        
        System.out.println("c==d?" + (c==d));
        System.out.println("d==e?" + (d==e));
        System.out.println("c==f?" + (c==f));
        System.out.println("c==m?" + (c==m));
        System.out.println("c==n?" + (c==n));
        System.out.println("d==str1?" + (d==str1));
        System.out.println("str1==str2?" + (str1==str2));
        System.out.println("e==str1?" + (e==str1));

输出结果:

c==d?true
d==e?false
c==f?true
c==m?true
c==n?true
d==str1?false
str1==str2?false
e==str1?false


程序中用来存放数据的内存分为四块
1、全局区(静态区)(static)
2、文字常量区 :常量字符串就是放在这块区域,即是我们常说起的常量池。
3、栈区(stack):存放函数的参数值,局部变量的值等。
4、堆区(heap) : 存放对象

当我们定义字符串
String a = "a";
a在栈区,“a”是字符串常量,在常量池中
String b = "b";
b在栈区,“b”在常量池
String c="ab";
c在栈区,“ab”在常量池
String d="ab";
d在栈区,这个时候常量池里已经有"ab",所以直接使用已经有的那个“ab”
所以这个时候c和d都指向的常量池里面的同一个“ab”
String e=a+b;
e在栈区,a+b实际上产生了一个新的String对象,既然是String对象,所以结果“ab”放在堆区中,即e指向的是堆里的“ab”
这样的情况下,c==d为true,c==e为false

另外,如果定义的是字符串对象
String str1 = new String("ab");
str1在栈区,创建的“ab”字符串对象在堆区
String str2 = new String("ab");
str2在栈区,又创建的一个新的“ab”对象也在堆区,不过和刚才的“ab”不是同一个。
相当于堆区中有两个字符串对象,不过正好内容都是“ab”而已。
所以str1==str2为false

常量池里面放着的常量字符串可以重复使用,但是必须是你直接使用的该字符串,像a+b这种形式虽然得到的结果是“ab”,但并不是使用的字符串常量“ab”。
关于String e=a+b,还有一种说法是经过编译器优化之后,结果为“ab”,则编译器直接从常量池中拿出“ab”常量,将引用赋值给e。我使用的JDK是1.6.0_34,得出的结果是d==e?false,说明这个版本的JVM没有进行上述的编译器优化。
(后期修正:对于String e = a + b;编译器是存在优化的。但是编译器的优化是在编译期,由于a和b都是变量字符串,在编译期无法确定其具体值,故编译期无法优化。如果a和b都使用了final修饰并且赋了初始值,即a和b是有初始值的常量字符串,则在编译期就能确定其值,编译器就会将e的值优化为"ab"。关于JVM常量池和字符串优化更详细的解释,请参考我的另一篇博文: String放入运行时常量池的时机与String.intern()方法解惑.
以上绝大部分内容摘抄了http://blog.sina.com.cn/s/blog_4b622a8e0100c296.html处的分析。

二、StringBuilder&StringBuffer
看了以下两个帖子,都写得挺详细的,大家可以去看看:
http://blog.csdn.net/kingzone_2008/article/details/9220691
http://www.cnblogs.com/dolphin0520/p/3778589.html

看了看源码,自己的一点理解:
1)
StringBuffer是线程安全的,StringBuilder是非安全的,在大部分情况下我们都不需要考虑多线程问题,所以用StringBuilder效率会高一点点,但当需要多线程的时候,就要使用StringBuffer了。以下英文是StringBuilder源码里的注释,也给出了使用建议。
This class provides an API compatible
 * with <code>StringBuffer</code>, but with no guarantee of synchronization.
 * This class is designed for use as a drop-in replacement for
 * <code>StringBuffer</code> in places where the string buffer was being
 * used by a single thread (as is generally the case).   Where possible,
 * it is recommended that this class be used in preference to
 * <code>StringBuffer</code> as it will be faster under most implementations.
2)
        StringBuilder sb  =  new  StringBuilder();
        sb.append(  "AAAA" );
        String appendStr =  null ;
        sb.append(appendStr);
        sb.append(  "BBBB" );
        System.  out .println(  "sb:"  + sb.toString());

         控制台输出结果: sb:AAAAnullBBBB
         这是因为对于append方法,当参数为null时,StringBuilder&StringBuffer中的处理是默认将“null”字符串加入sb的后面。 



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值