For string concatenation we can use either the concat()
or concat operator (+)
.
I have tried the following performance test and found concat()
is faster and a memory efficient way for string concatenation.
String concatenation comparison for 100,000 times:
String str = null; //------------Using Concatenation operator------------- long time1 = System.currentTimeMillis(); long freeMemory1 = Runtime.getRuntime().freeMemory(); for(int i=0; i<100000; i++){ str = "Hi"; str = str+" Bye"; } long time2 = System.currentTimeMillis(); long freeMemory2 = Runtime.getRuntime().freeMemory(); long timetaken1 = time2-time1; long memoryTaken1 = freeMemory1 - freeMemory2; System.out.println("Concat operator :" + "Time taken =" + timetaken1 + " Memory Consumed =" + memoryTaken1); //------------Using Concat method------------- long time3 = System.currentTimeMillis(); long freeMemory3 = Runtime.getRuntime().freeMemory(); for(int j=0; j<100000; j++){ str = "Hi"; str = str.concat(" Bye"); } long time4 = System.currentTimeMillis(); long freeMemory4 = Runtime.getRuntime().freeMemory(); long timetaken2 = time4-time3; long memoryTaken2 = freeMemory3 - freeMemory4; System.out.println("Concat method :" + "Time taken =" + timetaken2 + " Memory Consumed =" + memoryTaken2);
Result
Concat operator: Time taken = 31; Memory Consumed = 2259096 Concat method : Time taken = 16; Memory Consumed = 299592
If concat()
is faster than the operator then when should we use concatenation operator (+)
?
concat() method takes only one argument of string and concatenates it with other string. + operator takes any number of arguments and concatenates all the strings.
There are two ways to concatenate strings in Java: By + (String concatenation) operator. By concat() method.
The CONCAT function combines the text from multiple ranges and/or strings, but it doesn't provide delimiter or IgnoreEmpty arguments. CONCAT replaces the CONCATENATE function. However, the CONCATENATE function will stay available for compatibility with earlier versions of Excel.
Concatenation is the process of combining two or more strings to form a new string by subsequently appending the next string to the end of the previous strings. In Java, two strings can be concatenated by using the + or += operator, or through the concat() method, defined in the java. lang. String class.
The concat method always produces a new String with the result of concatenation.
The plus operator is backed by StringBuilder creation, appending all String values you need and further toString() calling on it.
So, if you need to concatenate two values, concat() will be better choice. If you need to concatenate 100 values, you should use the plus operator or explicitly use StringBuilder (e.g. in case of appending in a cycle).
In fact s1 + s2
and s1.concat(s2)
are very different.
s1 + s2
is converted by javac into
(new StringBuilder(String.valueOf(s1)).append(s2).toString();
You can see it if you decompile .class. This construct is not very efficient; it involves up to three new char[]
allocations and three char[]
copy operations.
s1.concat(s2)
is always one new char[]
+ one copy operation, see String.java
public String concat(String str) { int otherLen = str.length(); if (otherLen == 0) { return this; } char buf[] = new char[count + otherLen]; getChars(0, count, buf, 0); str.getChars(0, otherLen, buf, count); return new String(0, count + otherLen, buf); }
Note that new String(int, int, char[])
is String
's package private constructor. It uses char buf[]
directly, without the usual copying to ensure the buf
invisibility for the String
immutability.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With