To concatenate String
we often use StringBuilder
instead of String
+ String
, but also we can do the same with String.format
which returns the formatted string by given locale, format and arguments.
Examples:
Concatenate the string with StringBuilder
String concatenateStringWithStringBuilder(String name, String lName, String nick) { final StringBuilder sb = new StringBuilder("Contact {"); sb.append(", name='").append(name) .append(", lastName='").append(lName) .append(", nickName='").append(nick) .append('}'); return sb.toString(); }
Concatenate the string with StringFormat:
String concatenateStringWithStringFormat(String name, String lName, String nick) { return String.format("Contact {name=%s, lastName=%s, nickName=%s}", name, lName, nick); }
In performance, is String.Format
as efficient as StringBuilder
? Which one is better to concatenate strings and why?
UPDATE
I checked the similar question, but doesn´t answer my question. So far I have used StringBuilder
to concatenate strings, should I follow it using? Or should I use String.format
? the question is which one is better and why?
Equals is more efficient, it will only check if the strings match. Contains will search the for that text inside the string.
Therefore, concatenation is much faster than String.
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.
StringBuilder has overhead, so string is faster for limited concatenations. If you are going to append or modify thousands of times (usually not the case) then StringBuilder is faster in those scenarios.
What is "better" solely depends on your requirements:
For instance String Builder
will be faster, but the code will be much more unreadable, and and it would be easier to make a mistake.
On the other hand String.format()
produces more readable code at the cost of performance.
JMH benchmark to illustrate the performance difference (notice that the string builder code is longer and very hard to understand how the resulting string would look like):
@Fork(1) @State(Scope.Benchmark) @OutputTimeUnit(TimeUnit.MILLISECONDS) @Measurement(iterations = 10) @Warmup(iterations = 10) @BenchmarkMode(Mode.Throughput) public class StringFormatBenchmark { private String name = "UserName"; private String lName = "LUserName"; private String nick = "UserNick"; @Benchmark public void stringFormat(Blackhole blackhole) { final String result = String.format("Contact {name=%s, lastName=%s, nickName=%s}", name, lName, nick); blackhole.consume(result); } @Benchmark public void stringBuilder(Blackhole blackhole) { final StringBuffer sb = new StringBuffer("Contact {"); sb.append(", name='").append(name) .append(", lastName='").append(lName) .append(", nickName='").append(nick) .append('}'); final String result = sb.toString(); blackhole.consume(result); } }
And the results:
Benchmark Mode Cnt Score Error Units StringFormatBenchmark.stringBuilder thrpt 10 10617.210 ± 157.302 ops/ms StringFormatBenchmark.stringFormat thrpt 10 960.658 ± 7.398 ops/ms
For non performance critical code I prefer using the String.format()
, because it's easier and more pleasant to use. Also it's visible what the resulting string would look like, by simply looking at the pattern. If I'm doing a performance critical code, or something that has to have a low GC impact, I would use a StringBuilder
because it's faster and can be reused.
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