String与StringBuffer区别、StringBuffer每次append与append内部使用+号的区别

在日常的开发工作,特别是在Dao层类里面写SQL的时候,经常看到有人通过创建一个String对象,然后不断用+号链接。如:

String sql = "We always knew our daughter Kendall was going be a performer of some sort" 
        + "We always knew our daughter Kendall was going be a performer of some sort"
        + "We always knew our daughter Kendall was going be a performer of some sort"
        + "We always knew our daughter Kendall was going be a performer of some sort"
        + "We always knew our daughter Kendall was going be a performer of some sort"
        + "We always knew our daughter Kendall was going be a performer of some sort";

经常听到有同事在讨论String和StringBuffer之间的区别,百度上也有很多程序侠们发表过二者的区别,甚至通过代码来证明两者的时间消耗差距,但都只是在讨论String 与 StringBuffer的差异,其实StringBuffer的使用还有另外一种情况需要比较和注意,百度上也没见有人做过这类的研究。就是在StringBuffer的append内部使用+号的情况与每次.append的的区别和性能差异。如:

//第1种 多次通过append连接
StringBuffer sb = new StringBuffer();
  sb.append("We always knew our daughter Kendall was going be a performer of some sort")
	.append("We always knew our daughter Kendall was going be a performer of some sort")
	.append("We always knew our daughter Kendall was going be a performer of some sort")
	.append("We always knew our daughter Kendall was going be a performer of some sort")
	.append("We always knew our daughter Kendall was going be a performer of some sort")
	.append("We always knew our daughter Kendall was going be a performer of some sort")
	.append("We always knew our daughter Kendall was going be a performer of some sort")
	.append("We always knew our daughter Kendall was going be a performer of some sort");


//第2种 在append内部使用+号
StringBuffer sb = new StringBuffer();
sb.append("We always knew our daughter Kendall was going be a performer of some sort" 
    + "We always knew our daughter Kendall was going be a performer of some sort"
    + "We always knew our daughter Kendall was going be a performer of some sort"
    + "We always knew our daughter Kendall was going be a performer of some sort"
    + "We always knew our daughter Kendall was going be a performer of some sort"
    + "We always knew our daughter Kendall was going be a performer of some sort");

为了了解String、StringBuffer,以及StringBuffer的append和append内部使用+号这三种使用方式性能消耗情况,我通过代码实验在同等条件下最终得到以下结果:

使用string的时间是:39023毫秒!
使用StringBuffer的时间是:10毫秒!
使用StringBuffer内+号的时间是:6毫秒!

通过上述实验结果,可以看出StringBuffer比String性能好,而StringBuffer的两种用法中,内部+号性能最好。其实看过Java底层代码编译源码的应该知道,在编译过程中String对象其实最终还是通过创建StringBuffer对象,String每次+操作都会new一个StringBuffer来进行拼接,所以在这个过程中+的越多,new的StringBuffer对象就越多。而在StringBuffer内部使用+号连接在编译过程中,其实是不会多次创建StringBuffer对象的。

关于它们的区别我这里就不做深入介绍了,比较详细的深入的介绍网上一堆,在这里只是想通过实验来告诉程序侠们它们在用法上的性能区别有多大。因为每种用法其实需要结合你的使用场景来决定的,如果涉及大量的字符拼接的使用StringBuffer性能是最好的也建议你这样操作。

以下是实验代码:

public static void main(String[] args) {
    String str = "";
    StringBuffer sb = new StringBuffer();
	long start = 0L;
	long end = 0L;
	start = System.currentTimeMillis();
	for (int i = 0; i < 9999; i++) {
    str += "We always knew our daughter Kendall was going be a performer of some sort" 
        + "We always knew our daughter Kendall was going be a performer of some sort"
        + "We always knew our daughter Kendall was going be a performer of some sort"
        + "We always knew our daughter Kendall was going be a performer of some sort"
        + "We always knew our daughter Kendall was going be a performer of some sort"
        + "We always knew our daughter Kendall was going be a performer of some sort";
	}
	end = System.currentTimeMillis();
	System.out.println("使用string的时间是:" + (end - start) + "毫秒!");

	start = System.currentTimeMillis();
	for (int i = 0; i < 9999; i++) {
    sb.append("We always knew our daughter Kendall was going be a performer of some sort")
	.append("We always knew our daughter Kendall was going be a performer of some sort")
	.append("We always knew our daughter Kendall was going be a performer of some sort")
	.append("We always knew our daughter Kendall was going be a performer of some sort")
	.append("We always knew our daughter Kendall was going be a performer of some sort")
	.append("We always knew our daughter Kendall was going be a performer of some sort")
	.append("We always knew our daughter Kendall was going be a performer of some sort")
	.append("We always knew our daughter Kendall was going be a performer of some sort");
	}
	end = System.currentTimeMillis();
	System.out.println("使用StringBuffer的时间是:" + (end - start) + "毫秒!");

	start = System.currentTimeMillis();
	for (int i = 0; i < 9999; i++) {
    sb.append("We always knew our daughter Kendall was going be a performer of some sort" 
    + "We always knew our daughter Kendall was going be a performer of some sort"
    + "We always knew our daughter Kendall was going be a performer of some sort"
    + "We always knew our daughter Kendall was going be a performer of some sort"
    + "We always knew our daughter Kendall was going be a performer of some sort"
    + "We always knew our daughter Kendall was going be a performer of some sort");
	}
	end = System.currentTimeMillis();
	System.out.println("使用StringBuffer内+号的时间是:" + (end - start) + "毫秒!");
}

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

天使马克

写博客不易,客观打赏一下吧!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值