Converting Primitive Types to String and Efficient String Concatenation in Java
This article explains how to convert primitive types to strings in Java, discusses why simple concatenation with an empty string is inefficient, compares different splitting implementations, benchmarks various string concatenation methods including String.concat, the '+' operator, and StringBuilder, and recommends using String.valueOf and the OptimizeStringConcat JVM option for better performance.
Converting Primitive Types to String
In Java you often need to convert primitive values to strings; using an empty string concatenation ("" + value) creates a String but is inefficient because the compiler translates the expression into a StringBuilder chain, allocating multiple objects.
The compiler rewrites string_exp + any_exp into
new StringBuilder().append(string_exp).append(any_exp).toString(), and each additional '+' adds another append call.
When the concatenated part exceeds the default 16‑character buffer, the StringBuilder must expand its internal array, further increasing overhead. Using String.valueOf or the wrapper class's toString avoids creating the extra StringBuilder.
Splitting Strings Efficiently
Three split implementations are presented: the original split using a String separator, split2 that concatenates an empty string with a char separator, and split3 that directly uses the char overload of String.indexOf. Benchmarks on a 10‑million‑iteration test show that split3 is the fastest.
split
split2
split3
Java 6
4.65 sec
10.34 sec
3.8 sec
Java 7
6.72 sec
8.29 sec
4.37 sec
String Concatenation Methods
Three ways to concatenate many strings are compared: String.concat, the '+' operator, and StringBuilder.append. Benchmarks demonstrate that StringBuilder is orders of magnitude faster (≈0.01 s) than the other two (≈10 s and >40 s).
String.concat
+
StringBuilder.append
Result
10.145 sec
42.677 sec
0.012 sec
Since Java 6 u20, the JVM option -XX:+OptimizeStringConcat rewrites '+' operations into a more efficient implementation similar to String.concat. Enabling this option makes '+' performance comparable to String.concat, and even speeds up explicit StringBuilder usage.
+
+
new StringBuilder(21)
new StringBuilder(21)
switch off
switch on
switch off
switch on
Time
0.958 sec
0.494 sec
0.663 sec
0.494 sec
Conclusion
Avoid converting primitives by concatenating with an empty string; prefer String.valueOf or wrapper toString.
Use StringBuilder for repeated concatenation and replace legacy StringBuffer where thread safety is not required.
Enable -XX:+OptimizeStringConcat on Java 6 or rely on its default activation in newer Java 7 releases for better '+' performance.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Qunar Tech Salon
Qunar Tech Salon is a learning and exchange platform for Qunar engineers and industry peers. We share cutting-edge technology trends and topics, providing a free platform for mid-to-senior technical professionals to exchange and learn.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
