Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String assembly by StringBuilder vs StringWriter and PrintWriter

Tags:

java

I recently encountered an idiom I haven't seen before: string assembly by StringWriter and PrintWriter. I mean, I know how to use them, but I've always used StringBuilder. Is there a concrete reason for preferring one over the other? The StringBuilder method seems much more natural to me, but is it just style?

I've looked at several questions here (including this one which comes closest: StringWriter or StringBuilder ), but none in which the answers actually address the question of whether there's a reason to prefer one over the other for simple string assembly.

This is the idiom I've seen and used many many times: string assembly by StringBuilder:

public static String newline = System.getProperty("line.separator"); public String viaStringBuilder () {    StringBuilder builder = new StringBuilder();    builder.append("first thing").append(newline);  // NOTE: avoid doing builder.append("first thing" + newline);    builder.append("second thing").append(newline);    // ... several things    builder.append("last thing").append(newline);    return builder.toString(); } 

And this is the new idiom: string assembly by StringWriter and PrintWriter:

public String viaWriters() {    StringWriter stringWriter = new StringWriter();    PrintWriter printWriter = new PrintWriter(stringWriter);    printWriter.println("first thing");    printWriter.println("second thing");    // ... several things    printWriter.println("last thing");    printWriter.flush();    printWriter.close();    return stringWriter.toString(); } 

Edit It appears that there is no concrete reason to prefer one over the other, so I've accepted the answer which best matches my understanding, and +1ed all the other answers. In addition, I posted an answer of my own, giving the results of the benchmarking I ran, in response to one of the answers. Thanks to all.

Edit again It turns out that there is a concrete reason to prefer one (specifically the StringBuilder) over the other. What I missed the first time was the addition of the newline. When you add a newline (as above, as a separate append), it's slightly faster - not hugely, but coupled with the clarity of intent, it's definitely better. See my answer below for the improved timings.

like image 215
CPerkins Avatar asked Jun 05 '10 14:06

CPerkins


People also ask

What is StringWriter and PrintWriter in Java?

The write(String) method of PrintWriter Class in Java is used to write the specified String on the stream. This String value is taken as a parameter. Syntax: public void write(String string) Parameters: This method accepts a mandatory parameter string which is the String to be written in the Stream.

What is the best for using StringBuilder instead of 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.

What is StringWriter?

public class StringWriter extends Writer. A character stream that collects its output in a string buffer, which can then be used to construct a string. Closing a StringWriter has no effect. The methods in this class can be called after the stream has been closed without generating an IOException.


1 Answers

StringWriter is what you use when when you want to write to a string, but you're working with an API that expects a Writer or a Stream. It's not an alternative, it's a compromise: you use StringWriter only when you have to.

like image 130
Alan Moore Avatar answered Sep 20 '22 12:09

Alan Moore