概述
/**
* 字符串拼接
*/
public class StringAddDemo {
public static void main(String[] args){
testStringAdd();
testConcat();
testStringBuilder();
testStringBuffer();
//+:676
//concat:243
//StringBuilder:9
//StringBuffer:1
}
public static void testStringAdd(){
String s = "";
long begin = System.currentTimeMillis();
for (int i = 0; i < 10000; i++) {
s = s + i;
}
long end = System.currentTimeMillis();
System.out.println("+:" + (end-begin));
}
//连接
public static void testConcat(){
String s = "";
long begin = System.currentTimeMillis();
for (int i = 0; i < 10000; i++) {
s = s.concat(String.valueOf(i));
}
long end = System.currentTimeMillis();
System.out.println("concat:" + (end-begin));
}
//synchronization
public static void testStringBuffer(){
StringBuffer sb = new StringBuffer();
long begin = System.currentTimeMillis();
for (int i = 0; i < 10000; i++) {
sb.append(i);
}
sb.toString();
long end = System.currentTimeMillis();
System.out.println("StringBuffer:" + (end-begin));
}
public static void testStringBuilder(){
StringBuilder sb = new StringBuilder();
long begin = System.currentTimeMillis();
for (int i = 0; i < 10000; i++) {
sb.append(i);
}
sb.toString();
long end = System.currentTimeMillis();
System.out.println("StringBuilder:" + (end-begin));
}
}
最后
以上就是乐观白开水为你收集整理的java拼接字符串 判断内容_java--字符串拼接比较的全部内容,希望文章能够帮你解决java拼接字符串 判断内容_java--字符串拼接比较所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复