Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 828131
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T03:40:53+00:00 2026-05-15T03:40:53+00:00

I recently encountered an idiom I haven’t seen before: string assembly by StringWriter and

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T03:40:54+00:00Added an answer on May 15, 2026 at 3:40 am

    Stylistically, the StringBuilder approach is cleaner. It is fewer lines of code and is using a class that was specifically designed for the purpose of building strings.

    The other consideration is which is more efficient. The correct way to answer that would be to benchmark the alternatives, but (based on the source code) I would expect StringBuilder to be faster.

    A StringWriter uses a StringBuffer (rather than a StringBuilder) under the hood to hold the characters written to the "stream". Thus using a StringWriter for string assembly is going to incur StringBuffer‘s synchronization overhead, whether your application needs it or not. But if your application does need the synchronization, then you should consider using a StringBuffer directly.


    CPerkins has done some benchmarking (see his answer) and his results align with my intuition. They say that the difference between optimal StringBuilder vs StringWriter is ~5% which is likely to be insignificant for a typical application1. However, it is nice to know that the "style" and "performance" criteria are giving the same answer!

    1 – While a typical application doesn’t spend most of its time assembling strings, there are exceptions. However, one should not spend time optimizing this kind of thing based purely on guesswork. Profile your code first.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.