Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Best way to Concatenate Strings in java

    • 0
    • 1
    • 1
    • 2
    • 0
    • 0
    • 0
    • 0
    • 430
    Comment on it

    Optimized techniques for Concatenating Strings in java.

    We can concatenate multiple strings using either + operator or String.concat() or StringBuffer.append(). Which is the best one interms of performance?

    The choice depends on two scenarios.

    1- First scenario is compile time resolution vs run time resolution.

    2- Second scenario is using StringBuffer or String.

    In general, we think that StringBuffer.append() is better than + operator or String.concat() method. But this assumption is not true under certain conditions.

    1) First scenario: compile time resolution versus run time resolution

    Look at the following code and the output.

    /** This class shows the time taken by string concatenation at compile time and run time.*/
    public class ConcatenationTest1 {
     public static void main(String[] args){
               //Test the String Concatination
              long startTime = System.currentTimeMillis();
              for(int i=0;i<10;i++){
              String result = "This is"+ "test"+ "String";
              }
              long endTime = System.currentTimeMillis();
              System.out.println("Time taken for string concatenation using + operator : "
                                              + (endTime - startTime)+ " milli seconds");
    
               //Test the StringBuffer Concatination
                startTime = System.currentTimeMillis();
               for(int i=0;i<10;i++){
              StringBuffer result = new StringBuffer();
                                 result.append("This is");
                                result.append("test");
                               result.append("String");
    
               }
    
               endTime = System.currentTimeMillis();
              System.out.println("Time taken for String concatenation using StringBuffer : "
                                                 + (endTime - startTime)+ " milli seconds");
     }
    }
    
    
    The output of this code
    Time taken for String concatenation using + operator : 0 milli seconds
    Time taken for String concatenation using StringBuffer : 30 milli seconds
    

    Why plus(+) operator is faster than StringBuffer.append() method.

    Here the compiler concatenates at compile time as shown below. It does compile time resolution instead of runtime resolution. before compilation:

    String result = "This is"+"test"+"String";

    after compilation

    String result = "This is test String";

    Note --

    Run time resolution takes place when the value of the string is not known in advance where as compile time resolution happens when the value of the string is known in advance.

    2) Second scenario: Using StringBuffer instead of String.

    By looking following code, you will find StringBuffer is faster than String for concatenation which is opposite to above scenario.

    public class ConcatenationTest2 {
     public static void main(String[] args){
              //Test the String Concatenation using + operator
              long startTime = System.currentTimeMillis();
              String result = "rakesh";
              for(int i=0;i<10;i++){
              result += "rakesh";
              }
    
              long endTime = System.currentTimeMillis();
              System.out.println("Time taken for string concatenation using + operator : "
                                              + (endTime - startTime)+ " milli seconds");
    
    
              //Test the String Concatenation using StringBuffer
              long startTime1 = System.currentTimeMillis();
              StringBuffer result1 = new StringBuffer("rakesh");
              for(int i=0;i<10;i++){
              result1.append("rakesh");
              }
              long endTime1 = System.currentTimeMillis();
              System.out.println("Time taken for string concatenation using StringBuffer :  "
                                              + (endTime1 - startTime1)+ " milli seconds");
              }
    }
    
    The output of the code is
    
    Time taken for string concatenation using + operator : 210 milli seconds
    Time taken for String concatenation using String Buffer : 0 milli seconds
    

    It shows StringBuffer.append() is much more faster than String.

    The reason is both resolve at run time but the plus(+) operator resolution for string concatenation take more time because String uses different technique for concatenation then String Buffer to do this operation.

    Conclusion

    1.) Plus(+) operator gives best performance for String concatenation if Strings resolve at compile time.

    2.) String Buffer gives best performance for String concatenation if Strings resolve at run time.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: