BIG
DATA

JAVA

Effective Java Item - 51 : Beware the performance of string concatenation

Read more about »
  • Java 9 features
  • Read about Hadoop
  • Read about Storm
  • Read about Storm
 

Item 51: Beware the performance of string concatenation

Strings are immutable. String concatenation appends one String to the end of another. There are 4 Ways to concatenate String in Java:

  • Concatenation operator (+)
  • String.concat() function
  • StringBuffer class
  • StringBuilder class

Of the four, string concatenation operator (+) is a convenient way to perform string concatenation. You can use this operator to combine more than one String like "Core" + "Java" + "Guru", which will result in "CoreJavaGuru". You can use String literal or String variable, it will work in both scenario.

String concatenation doesn't modify any String object and always create a new String object. When you do string concatenation, it may return object from String pool if it exists but otherwise it will result in a new String object.

It is fine for generating a single line of output or for constructing the string representation of a small, fixed-size object, but it does not scale. Using the string concatenation operator repeatedly to concatenate n strings requires time quadratic in n. Never use this method while concatenating String in loop, it will result in lots of small String garbage. Lets see with an example

public class MyClass {	
	public static void main(String[] args) {

		int count = 1000;
		String str="";
		long startTime = System.nanoTime();    
		for (int i = 0; i < count; i++)
		{
			str = str + "core"; // String concatenation
		}
		long estimatedTime = System.nanoTime() - startTime;
		System.out.println("+ operator:" + estimatedTime);
		
		startTime = System.nanoTime();    
		for (int i = 0; i < count; i++)
		{
			str = str.concat("core");
		}
		estimatedTime = System.nanoTime() - startTime;
		System.out.println("concat method:" + estimatedTime);
		
		StringBuilder sb = new StringBuilder();
		startTime = System.nanoTime();    
		for (int i = 0; i < count; i++)
		{
			sb.append("core");
		}
		str=sb.toString();
		estimatedTime = System.nanoTime() - startTime;
		System.out.println("StringBuilder:" + estimatedTime);
		
		StringBuffer sbuf = new StringBuffer();
		startTime = System.nanoTime();    
		for (int i = 0; i < count; i++)
		{
			sbuf.append("core");
		}
		str=sbuf.toString();
		estimatedTime = System.nanoTime() - startTime;
		System.out.println("StringBuffer:" + estimatedTime);
	}
}

I have run the above code with loop count as 1000,2000,3000 and below are the results:

effective java item-51 effective java item-51

As you can see the difference in performance is dramatic. In the above code I am concatenating string in a loop. And the time taken to complete is different for "+" operator, concat method, StringBuilder, StringBuffer.

In the above code StringBuilder, StringBuffer is detuned to use a default-sized StringBuilder, it is still much faster than "+" operator.

As the no of loops increases difference in performance widens more and more.

effective java item-51

The quickest way of concatenating String in Java is by using concatenation operator ("+") and it works quite well if you just have to join one or two fixed size String, but if you have to join thousands of String or you are performing String concatenation in loop then performance of concatenation operator is not good. Lets have a look:

public class MyClass {	
	public static void main(String[] args) {
		
		long startTime = System.nanoTime();    
		String str2 = "Core" + " Java" + " Guru";
		long estimatedTime = System.nanoTime() - startTime;
		System.out.println("concat method:" + estimatedTime);
		
		startTime = System.nanoTime();    
		StringBuilder sb2 = new StringBuilder();
		sb2.append("Core");
		sb2.append(" Java");
		sb2.append(" Guru");
		str2 = sb2.toString();
		estimatedTime = System.nanoTime() - startTime;
		System.out.println("concat method:" + estimatedTime);
	}
}
effective java item-51

So, use + operator for String concatenation single line of output or for constructing the string representation of a small, fixed-size object.

Summary

Don't use the string concatenation operator to combine more than a few strings unless performance is irrelevant. Use StringBuilder's append method instead. This is right way to join multiple String in Java. Why? because it represent a mutable String and when you concatenate multiple small String, it won't generate temporary intermediate String object. This result in lot of memory saving and reduce garbage collection time.

Note:You should always use StringBuilder instead of StringBuffer because it provides the same functionality but without any synchronization overhead, which means faster operation.

Points to remember
  • Don't use + operator for String concatenation in loop.
  • Use + operator for String concatenation single line of output or for constructing the string representation of a small, fixed-size object.
  • Always use StringBuilder for concatenation of multiple String.
  • Always initialize StringBuilder with proper capacity.