Home > Java > javaTutorial > Java Example - Connection String

Java Example - Connection String

黄舟
Release: 2017-02-22 09:53:09
Original
1227 people have browsed it

The following example demonstrates concatenating strings through the "+" operator and the StringBuffer.append() method, and compares their performance:

//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");
   }}
Copy after login

The output result of the above code example is:

字符串连接 - 使用 + 操作符 : 0 ms 
字符串连接 - 使用 StringBuffer : 42 ms
Copy after login

The above is the content of the Java example-connection string. For more related content, please pay attention to the PHP Chinese website (m.sbmmt.com)!


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template