[Java] Speed comparison of string concatenation


It has already been widely known from a long time ago (* around Java 1.5) that "string concatenation is not" + ", but using StringBuffer speeds up processing". Therefore, I still basically use StringBuffer and StringBuilder for string concatenation, but I suddenly wondered "How fast is the processing speed of the concat method of the String class?", So this time, these processing speeds I tried to compare.

testing environment

Code used for testing


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

	public static void plus() {

		long start = System.currentTimeMillis();
		String txt = "abc";

		for (int i=0; i<100000; i++) {
			txt += "abc";

		long end = System.currentTimeMillis();
		System.out.println("+:" + (end - start) + "ms");

	public static void concat() {

		long start = System.currentTimeMillis();
		String txt = "abc";

		for (int i=0; i<100000; i++) {
			txt = txt.concat("abc");

		long end = System.currentTimeMillis();
		System.out.println("concat:" + (end - start) + "ms");

	public static void builder() {

		long start = System.currentTimeMillis();
		StringBuilder sb = new StringBuilder("abc");

		for (int i=0; i<100000; i++) {

		String txt = sb.toString();
		long end = System.currentTimeMillis();
		System.out.println("StringBuilder:" + (end - start) + "ms");

	public static void buffer() {

		long start = System.currentTimeMillis();
		StringBuffer sb = new StringBuffer("abc");

		for (int i=0; i<100000; i++) {

		String txt = sb.toString();
		long end = System.currentTimeMillis();
		System.out.println("StringBuffer:" + (end - start) + "ms");

test results

Processing content processing time
+Combined by 7439ms
String.concat()Combined by 2990ms
Join with StringBuilder 2ms
Join by StringBuffer 4ms

Consideration of test results

Natural level story

Why is String.concat () a bit faster than "join by +"?


String.class excerpt

//Source obtained using the Eclipse Class Decompiler plugin.

public final class String implements Serializable, Comparable<String>, CharSequence {
   private final char[] value;

   public String concat(String arg0) {
      int arg1 = arg0.length();
      if(arg1 == 0) {
         return this;
      } else {
         int arg2 = this.value.length;
         char[] arg3 = Arrays.copyOf(this.value, arg2 + arg1);
         arg0.getChars(arg3, arg2);
         return new String(arg3, true);

   void getChars(char[] arg0, int arg1) {
      System.arraycopy(this.value, 0, arg0, arg1, this.value.length);

Combine with +

The Java language provides special support for string concatenation operators (+) and other object-to-string conversions. String concatenation is implemented using the StringBuilder (or StringBuffer) class and its append method.


Recommended Posts

[Java] Speed comparison of string concatenation
[Note] Java: Measures the speed of string concatenation
[Java] Correct comparison of String type
[Java] Comparison of String type character strings
[Java] String comparison and && and ||
The story of low-level string comparison in Java
[Java] Type conversion speed comparison
[Java] String join execution speed comparison (+ operator vs StringBuilder)
Various methods of Java String class
Java string
Regarding String type equivalence comparison in Java
[Java / Swift] Comparison of Java Interface and Swift Protocol
Item 63: Beware the performance of string concatenation
Speed comparison test for String, StringBuilder, StringBuffer
[Java] String padding
MyBatis string comparison
[Java] Map comparison
Java string processing
Java framework comparison
Split string (Java)
[Java] Overview of Java
[Java] Get the length of the surrogate pair string
[Java] The confusing part of String and StringBuilder
[Note] Java: Speed of List processing by purpose
Why Java String typeclass comparison (==) cannot be used
[Verification] Comparison of Spring Boot vs Micronaut boot speed
Expired collection of java
Predicted Features of Java
[Java] Significance of serialVersionUID
[Java] Meaning of Public static void main (String [] args)
[String type vs String Builder] Difference in processing speed in string concatenation
The comparison of enums is ==, and equals is good [Java]
NIO.2 review of java
Review of java Shilber
Java version notation comparison
Speed comparison at the time of generation at the time of date conversion
Java string multiple replacement
java --Unification of comments
[java.io] Summary of Java string input (InputStream, Reader, Scanner)
History of Java annotation
[Note] Java: String search
[Note] Java: String survey
java (merits of polymorphism)
[Java] About Objects.equals () and Review of String comparisons (== and equals)
About Java String class
NIO review of java
[Development] Java framework comparison
Equivalence comparison of Java wrapper classes and primitive types
[Java] Three features of Java
[Null safety] Kotlin Java comparison memo Correct use of Null safety
Summary of Java support 2018
[Java] Handling of character strings (String class and StringBuilder class)
[Java] Comparison method of character strings and comparison method using regular expressions
Comparison of how to write Callback function (Java, JavaScript, Ruby)
"Understanding this kind of thing No.2" Volume of comparison operators [Java]
Initialization with an empty string to an instance of Java String type
[Java] Try editing the elements of the Json string using the library
[Introduction to Java] Handling of character strings (String class, StringBuilder class)
About an instance of java
[Java] Mirage-Basic usage of SQL
[Java] Beginner's understanding of Servlet-②