May be I am splitting hair, but I was wondering in the following case:
String newString = a + b + c; //case 1
String newString = a.concat(b).concat(c);
On performance Analysis with JDK 1.8, I found case 2 as the winner in terms of performance.
class Solution {
public static void main(String[] args) {
String s ="ABC", s1 = "", s2 = "", s3 = "";
long t1 = System.nanoTime();
for(int i = 1; i < 11; i++) {
s1 = s1 + s;
}
System.out.println(System.nanoTime() - t1);
System.out.println(s1);
long t2 = System.nanoTime();
for(int i = 1; i < 11; i++) {
s2 = s2.concat(s);
}
System.out.println(System.nanoTime() - t2);
System.out.println(s2);
long t3 = System.nanoTime();
StringBuilder sb = new StringBuilder();
for(int i = 1; i < 11; i++) {
sb.append(s);
}
s3 = sb.toString();
System.out.println(System.nanoTime() - t3);
System.out.println(s3);
}
}
Results :
40615
ABCABCABCABCABCABCABCABCABCABC
9157
ABCABCABCABCABCABCABCABCABCABC
20243
ABCABCABCABCABCABCABCABCABCABC