Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best practices/performance: mixing StringBuilder.append with String.concat

I'm trying to understand what the best practice is and why for concatenating string literals and variables for different cases. For instance, if I have code like this

StringBuilder sb = new StringBuilder("AAAAAAAAAAAAA")
    .append(B_String).append("CCCCCCCCCCC").append(D_String)
    .append("EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE")
    .append("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF");

Is this the way to do it? From this post, I noticed that the + operator on Strings creates a new instance of StringBuilder, concatenates the operands, and returns a String conversion, which seems like a lot more work than just calling .append(); so if that's true, then that is out of the question. But what about String.concat()? Is it proper to use .append() for every concatenation? Or just for variables, and literals are okay to append with .concat()?

StringBuilder sb = new StringBuilder("AAAAAAAAAAAAA")
    .append(B_String.concat("CCCCCCCCCCC")).append(D_String
    .concat("EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE")
    .concat("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"));

What are the general rules for best practices and performance for going about these situations? Is my assumption correct on + and it should really just not be used?

like image 856
Nick Rolando Avatar asked Oct 20 '22 14:10

Nick Rolando


People also ask

Which is faster string concatenation or the StringBuilder class?

Note that regular string concatenations are faster than using the StringBuilder but only when you're using a few of them at a time. If you are using two or three string concatenations, use a string.

What is the most efficient way to concatenate many strings together?

When concatenating three dynamic string values or less, use traditional string concatenation. When concatenating more than three dynamic string values, use StringBuilder . When building a big string from several string literals, use either the @ string literal or the inline + operator.

Why is StringBuilder better than string concatenation?

Reason being : The String concatenate will create a new string object each time (As String is immutable object) , so it will create 3 objects. With String builder only one object will created[StringBuilder is mutable] and the further string gets appended to it.

What is time complexity of StringBuilder append?

It is O(1) when appending single characters. A StringBuilder is like an ArrayList. When you append a single item the cost is O(1).


1 Answers

+ operator

String s = s1 + s2

Behind the scenes this is translated to:

String s = new StringBuilder(s1).append(s2).toString();

Imagine how much extra work it adds if you have s1 + s2 here:

stringBuilder.append(s1 + s2)

instead of:

stringBuilder.append(s1).append(s2)

Multiple strings with +

Worth to note that:

String s = s1 + s2 + s3 + ... +sN

is translated to:

String s = new StringBuilder(s1).append(s2).append(s3)...apend(sN).toString();

concat()

String s = s1.concat(s2);

String creates char[] array that can fit both s1 and s2. Copies s1 and s2 contents to this new array. Actually requires less work then + operator.

StringBuilder.append()

Maintains an internal char[] array that grows when needed. No extra char[] is created if the internal one is sufficiently big.

stringBuilder.append(s1.concat(s2))

is also performing poorly because s1.concat(s2) creates an extra char[] array and copies s1 and s2 to it just to copy that new array contents to internal StringBuilder char[].

That being said you should use append() all the time and append raw strings (your first code snippet is correct).

like image 99
Tomasz Nurkiewicz Avatar answered Nov 11 '22 17:11

Tomasz Nurkiewicz