String 拼接的方法选择

在拼接静态字符串时,尽量用 +,因为通常编译器会对此做优化,如:

 String test = "this " + "is " + "a " + "test " + "string"

编译器会把它视为:

 String test = "this is a test string"

在拼接动态字符串时,尽量用 StringBuffer 或 StringBuilder的 append,这样可以减少构造过多的临时 String 对象。

测试代码:(按照附录1修改)

 public class teststring {

     public void testPlus() {
String s = "";
long ts = System.currentTimeMillis();
for (int i = 0; i < 10000; i++) {
s = s + String.valueOf(i);
}
long te = System.currentTimeMillis();
System.out.println("+ cost {" + ( te - ts) + "} ms");
} public void testConcat() {
String s = "";
long ts = System.currentTimeMillis();
for (int i = 0; i < 10000; i++) {
s = s.concat(String.valueOf(i));
}
long te = System.currentTimeMillis();
System.out.println("concat cost {" + (te - ts) + "} ms");
} public void testStringBuffer() {
StringBuffer sb = new StringBuffer();
long ts = System.currentTimeMillis();
for (int i = 0; i < 10000; i++) {
sb.append(String.valueOf(i));
}
sb.toString();
long te = System.currentTimeMillis();
System.out.println("StringBuffer cost {" + (te - ts) + "} ms");
} public void testStringBuilder() {
StringBuilder sb = new StringBuilder();
long ts = System.currentTimeMillis();
for (int i = 0; i < 100000; i++) {
sb.append(String.valueOf(i));
}
sb.toString();
long te = System.currentTimeMillis();
System.out.println("StringBuilder cost {" + (te - ts) + "} ms");
} public static void main(String[] args) {
teststring a = new teststring();
a.testConcat();
a.testPlus();
a.testStringBuffer();
a.testStringBuilder();
}
}

运行结果:

concat cost {} ms 113
+ cost {} ms 195
StringBuffer cost {} ms 2
StringBuilder cost {} ms 9

可见 存在大量的拼接动态字符串操作时,尽量用 StringBuffer 或 StringBuilder的 append。使用'+'运算符的开销是不可忍受的。

In general, if sb refers to an instance of a StringBuilder, then sb.append(x) has the same effect as sb.insert(sb.length(), x). Every string builder has a capacity. As long as the length of the character sequence contained in the string builder does not exceed the capacity, it is not necessary to allocate a new internal buffer. If the internal buffer overflows, it is automatically made larger.

Instances of StringBuilder are not safe for use by multiple threads. If such synchronization is required then it is recommended that StringBuffer be used.

附录:

Java 5种字符串拼接方式性能比较 http://blog.csdn.net/kimsoft/article/details/3353849

2 Java 性能优化之 String 篇  http://www.ibm.com/developerworks/cn/java/j-lo-optmizestring/

java String拼接的方法选择及性能分析的更多相关文章

  1. java String 中 intern方法的概念

    1. 首先String不属于8种基本数据类型,String是一个对象. 因为对象的默认值是null,所以String的默认值也是null:但它又是一种特殊的对象,有其它对象没有的一些特性. 2. ne ...

  2. Java 字符串拼接四种方式的性能比较分析

    一.简单介绍 编写代码过程中,使用"+"和"contact"比较普遍,但是它们都不能满足大数据量的处理,一般情况下有一下四种方法处理字符串拼接,如下: 1. 加 ...

  3. java String的intern()方法

    intern()方法用于将字符串对象加入常量池中. public native String intern(); intern()方法返回的是一个常量池中的String对象(即常量池中某个String ...

  4. java String 提供的方法

    String类的判断功能: * boolean equals(Object obj):比较字符串的内容是否相同,区分大小写 * boolean equalsIgnoreCase(String str) ...

  5. 十大基础排序算法[java源码+动静双图解析+性能分析]

    一.概述 作为一个合格的程序员,算法是必备技能,特此总结十大基础排序算法.java版源码实现,强烈推荐<算法第四版>非常适合入手,所有算法网上可以找到源码下载. PS:本文讲解算法分三步: ...

  6. 30天C#基础巩固------this,base,string中的方法,StringBuilder性能

    这里主要是记录下自己学习笔记,希望有个地方在以后可以看到自己走过的路. 关于之前多态的知识有一个口诀,很好理解里面的override和new,virtual关键字. "new则隐藏,over ...

  7. java String类 trim() 方法源码分析

    public String trim() {        int arg0 = this.value.length;   //得到此字符串的长度        int arg1 = 0;   //声 ...

  8. java String的各种方法及操作

    No. 方法名称 功能 字符与字符串 01 public String(char[] value) 将字符数组中所有内容变为字符串 02 public String(char[] value,int ...

  9. java.String中的方法

    (String) str.trim() 该方法返回一个复制该字符串的开头和结尾的白色空格去掉,或字符串,如果它没有头或尾空白. (Boolean) str.contains(str1) 判断 str ...

随机推荐

  1. Activiti 乱码问题

    新版本运行起来中文看起来正常了许多,至少生成图片过程中不会出现乱码了,但不出预料的再次遇到部署时乱码问题,除了保存时报错,还会导致流程实例的节点名称是乱码. 1. IE报错定义时错误提示: 元素类型 ...

  2. -other linker flags - 详解

    • 值:-objC,-all_load,-force_load

 • -objC: 在iOS 中,使用-all_load时,如果静态库中有类别时会出问题,使用其他两个值则不会有问题.

 • -al ...

  3. 398. Random Pick Index随机pick函数

    [抄题]: Given an array of integers with possible duplicates, randomly output the index of a given targ ...

  4. linux环境下搭建osm_web服务器二(Mapnik及apache2mod_tile配置):

    Mapnik及apache2mod_tile配置 上一篇,我们配置好了PostgreSQL服务器,导入了测试数据.今天,我们来配置 mapnik2 + apache2 + mod_tile 的WMS服 ...

  5. realsense pcl git

    https://github.com/Ext4FAT/Registration vc++ pcl realsense  矿泉水瓶子 https://github.com/dBeker/PCL-Real ...

  6. Linux 上安装 rlwrap

    1.安装rlwrap的初衷; 2.安装rlwrap工具和遇到的问题; 3.使用rlwrap 工具; 1.安装rlwrap的初衷: 在Windows 下使用SQLPLUS都是可以使用上下左右方向键前后左 ...

  7. Python 简单模块学习

    1. openpyxl / xlrd / xlwt  => 操作Excel 文件(xlsx格式) => xlrd + xlwt : 只能操作xls文件,分别负责读写, 暂时不讨论 => ...

  8. Linux Mint 17 搭建 JSP 环境

    一.配置Tomcat 服务器 1.下载 tomcat 2.解压后放到/usr/local目录下面 3.以root权限执行  chmod +x *.sh 4.启动 ./startup.sh#方式1 ./ ...

  9. Android动态加载--JVM 类加载机制

    动态加载,本质上是通过JVM类加载机制将插件模块加载到宿主apk中,并通过android的相关运行机制,实现插件apk的运行.因此熟悉JVM类加载的机制非常重要. 类加载机制:虚拟机把描述类的数据从C ...

  10. Ubuntu14.04下使用PPA安装php5.6,php7

    1.为了使用ppa(Personal Package Archives) 选安装依赖: # apt-get install python-software-properties 2.添加不同版本php ...