Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java concatenate to build string or format

I'm writing a MUD (text based game) at the moment using java. One of the major aspects of a MUD is formatting strings and sending it back to the user. How would this best be accomplished?

Say I wanted to send the following string:

You say to Someone "Hello!" - where "Someone", "say" and "Hello!" are all variables. Which would be best performance wise?

"You " + verb + " to " + user + " \"" + text + "\""

or

String.format("You %1$s to %2$s \"%3$s\"", verb, user, text)

or some other option?

I'm not sure which is going to be easier to use in the end (which is important because it'll be everywhere), but I'm thinking about it at this point because concatenating with +'s is getting a bit confusing with some of the bigger lines. I feel that using StringBuilder in this case will simply make it even less readable.

Any suggestion here?

like image 970
two13 Avatar asked Oct 15 '11 02:10

two13


People also ask

What is the best way to concatenate strings in Java?

Using + Operator The + operator is one of the easiest ways to concatenate two strings in Java that is used by the vast majority of Java developers. We can also use it to concatenate the string with other data types such as an integer, long, etc.

Should I use string format?

tl;dr. Avoid using String. format() when possible. It is slow and difficult to read when you have more than two variables.

Is string builder faster than string?

String is immutable whereas StringBuffer and StringBuilder are mutable classes. StringBuffer is thread-safe and synchronized whereas StringBuilder is not. That's why StringBuilder is faster than StringBuffer.

Does string concatenation create a new string in Java?

String Concatenation by concat() method in Java. This string concat() method concatenates or joins the specified string to the end of current string and creates a new string object.


1 Answers

If the strings are built using a single concatenation expression; e.g.

String s = "You " + verb + " to " + user + " \"" + text + "\"";

then this is more or less equivalent to the more long winded:

StringBuilder sb = new StringBuilder();
sb.append("You");
sb.append(verb);
sb.append(" to ");
sb.append(user);
sb.append(" \"");
sb.append(text );
sb.append('"');
String s = sb.toString();

In fact, a classic Java compiler will compile the former into the latter ... almost. In Java 9, they implemented JEP 280 which replaces the sequence of constructor and method calls in the bytecodes with a single invokedynamic bytecode. The runtime system then optimizes this1.

The efficiency issues arise when you start creating intermediate strings, or building strings using += and so on. At that point, StringBuilder becomes more efficient because you reduce the number of intermediate strings that get created and then thrown away.

Now when you use String.format(), it should be using a StringBuilder under the hood. However, format also has to parse the format String each time you make the call, and that is an overhead you don't have if you do the string building optimally.


Having said this, My Advice would be to write the code in the way that is most readable. Only worry about the most efficient way to build strings if profiling tells you that this is a real performance concern. (Right now, you are spending time thinking about ways to address a performance issue that may turn out to be insignificant or irrelevant.)

Another answer mentions that using a format string may simplify support for multiple languages. This is true, though there are limits as to what you can do with respect to such things as plurals, genders, and so on.


1 - As a consequence, hand optimization as per the example above might actually have negative consequences, for Java 9 or later. But this is a risk you take whenever you micro-optimize.

like image 128
Stephen C Avatar answered Sep 19 '22 15:09

Stephen C