Runoob-Java-高级教程-实例-字符串:14. Java 实例

ylbtech-Runoob-Java-高级教程-实例-字符串:14. Java 实例 - 连接字符串
1.返回顶部
1、

Java 实例 - 连接字符串

Java 实例 Java 实例

以下实例演示了通过 "+" 操作符和StringBuffer.append() 方法来连接字符串,并比较其性能:

StringConcatenate.java 文件

public class StringConcatenate {
    public static void main(String[] args){
        long startTime = System.currentTimeMillis();
        for(int i=0;i<5000;i++){
            String result = "This is"
            + "testing the"
            + "difference"+ "between"
            + "String"+ "and"+ "StringBuffer";
        }
        long endTime = System.currentTimeMillis();
        System.out.println("字符串连接" 
        + " - 使用 + 操作符 : " 
        + (endTime - startTime)+ " ms");
        long startTime1 = System.currentTimeMillis();
        for(int i=0;i<5000;i++){
            StringBuffer result = new StringBuffer();
            result.append("This is");
            result.append("testing the");
            result.append("difference");
            result.append("between");
            result.append("String");
            result.append("and");
            result.append("StringBuffer");
        }
        long endTime1 = System.currentTimeMillis();
        System.out.println("字符串连接" 
        + " - 使用 StringBuffer : "
        + (endTime1 - startTime1)+ " ms");
    }
}

以上代码实例输出结果为:

字符串连接 - 使用 + 操作符 : 0 ms
字符串连接 - 使用 StringBuffer : 6 ms

Java 实例 Java 实例

2、
2.返回顶部
1、
扩展案例1

案例扩展认知:+"为每个字符串变量赋值,公用一个内值,占用一份内存空间;"StringBuffer"每次新建一个新对象,内存分配新的空间,新分配5000份内存空间

public class StringConcatenate {
    public static void main(String[] args){
        long startTime = System.currentTimeMillis();
        String[] strArr = new String[500];
        for(int i=0;i<500;i++){
            String result = "This is";
            strArr[i]=String.valueOf(result.hashCode());
        }
        long endTime = System.currentTimeMillis();
        System.out.println("字符串连接" 
        + " - 使用 + 操作符 : " 
        + (endTime - startTime)+ " ms");
        System.out.println(strArr[0]+"
"+strArr[1]+"
"+strArr[2]);
        long startTime1 = System.currentTimeMillis();
        for(int i=0;i<500;i++){
            StringBuffer result = new StringBuffer();
            result.append("This is");
            strArr[i]=String.valueOf(result.hashCode());
        }
        long endTime1 = System.currentTimeMillis();
        System.out.println("字符串连接" 
        + " - 使用 StringBuffer : "
        + (endTime1 - startTime1)+ " ms");
        System.out.println(strArr[0]+"
"+strArr[1]+"
"+strArr[2]);
    }
}

打印结果:

字符串连接-使用+操作符:1ms
-1027042079
-1027042079
-1027042079
字符串连接-使用StringBuffer操作符:2ms1167165921
1442002549
1383884648
2、
扩展案例2

附上另一种角度的性能分析,当需要对字符串对象的长度进行变化时用 + 拼接的性能在循环时就会慢的慢的多,实际上 + 号拼接字符串也是通过 StringBuild 或 StringBuffer 实现的,但当进行频繁的修改本身时,+ 拼接会比直接用方法拼接产生更多的中间垃圾对象,耗用更多的内存,因此更推荐使用 StringBuild。其实我认为上述案例的性能分析是没有意义的,如果明确了要拼接的字符串的话,完全可以直接使用两种如下代码:

result =result +  "This is esting the difference between String and StringBuffer ";
或
result.append("This is esting the difference between String and StringBuffer" );
public class Main {
    public static void main(String[] args){
     String result1 = null;
     StringBuffer result = new StringBuffer();
        long startTime = System.currentTimeMillis();
        for(int i=0;i<5000;i++){
            result1 += "This is"
            + "testing the"
            + "difference"+ "between"
            + "String"+ "and"+ "StringBuffer";
        }
        long endTime = System.currentTimeMillis();
        System.out.println("字符串连接" 
        + " - 使用 + 操作符 : " 
        + (endTime - startTime)+ " ms");
        long startTime1 = System.currentTimeMillis();
        for(int i=0;i<5000;i++){
            
            result.append("This is");
            result.append("testing the");
            result.append("difference");
            result.append("between");
            result.append("String");
            result.append("and");
            result.append("StringBuffer");
        }
        long endTime1 = System.currentTimeMillis();
        System.out.println("字符串连接" 
        + " - 使用 StringBuffer : "
        + (endTime1 - startTime1)+ " ms");
    }
}

输出结果:

字符串连接 - 使用 + 操作符 : 1151 ms
字符串连接 - 使用 StringBuffer : 2 ms
3、
3.返回顶部
 
4.返回顶部
 
5.返回顶部
1、
2、
 
6.返回顶部
 
warn 作者:ylbtech
出处:http://ylbtech.cnblogs.com/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
原文地址:https://www.cnblogs.com/storebook/p/10647383.html