Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
308 views
in Technique[技术] by (71.8m points)

java - Is String concatenation on assignment efficient?

I know that using the "+" concatenation operator for building strings is very inefficient, and that is why it is recommended to use the StringBuilder class, but I was wondering if this kind of pattern is inefficient too?

String some = a + "" + b + "" + c + "" + d + "" + e;

I guess here the compiler will optimize the assignment fine, or not?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your premise “that using the "+" concatenation operator for building strings is very inefficient”, is not correct. First, string concatenation itself is not a cheap operation, as it implies creating a new string containing all concatenated strings, hence, needing to copy the character contents. But this does always apply, regardless of how you do it.

When you use the + operator, you’re telling what you want to do, without saying how to do it. Not even the Java Language Specification demands a particular implementation strategy, except that the concatenation of compile-time constants must be done at compile time. So for compile-time constants, the + operator is the most efficient solution1.

In practice, all commonly used compilers from Java?5 to Java?8 generate code using a StringBuilder under the hood (before Java?5, they used StringBuffer). This applies to statements like yours, so replacing it with a manual StringBuilder use would not gain much. You could be slightly better than the typical compiler generated code by providing a reasonable initial capacity, but that’s all.

Starting with Java?9, compilers generate an invokedynamic instruction which allows the runtime to provide the actual code performing the concatenation. This could be a StringBuilder code similar to the one used in the past, but also something entirely different. Most notably, the runtime provided code can access implementation specific features, which the application code could not. So now, the string concatenation via + can be even faster than StringBuilder based code.

Since this applies to a single concatenation expression only, when performing a string construction using multiple statements or even a loop, using a StringBuilder consistently during the entire construction may be faster than the multiple concatenation operations. However, since the code runs in an optimizing environment, with a JVM recognizing some of these patterns, not even that can be said for sure.

This is the time to remember the old rule, to only try to optimize performance, when there is an actual problem with the performance. And always verify with impartial measuring tools, whether an attempted optimization truly improves the performance. There are a lot of widespread myths, wrong or outdated, about performance optimization tricks.

1 except you have repeated parts and want to reduce the size of the class file


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...