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

  • SEARCH
  • Home
  • 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 9317347
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T02:51:22+00:00 2026-06-19T02:51:22+00:00

In this answer , it says (implies) that String concatenation is optimised into StringBuilder

  • 0

In this answer, it says (implies) that String concatenation is optimised into StringBuilder operations anyway, so when I write my code, is there any reason to write StringBuilder code in the source? Note that my use case is different from the OP’s question, as I am concatenating/appending hundreds-thousands of lines.

To make myself clearer: I am well-aware about the differences of each, it’s just that I don’t know if it’s worth actually writing StringBuilder code because it’s less readable and when its supposedly slower cousin, the String class, is converted automagically in the compilation process anyway.

  • 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-06-19T02:51:23+00:00Added an answer on June 19, 2026 at 2:51 am

    I think the use of StringBuilder vs + really depends on the context you are using it in.

    Generally using JDK 1.6 and above the compiler will automatically join strings together using StringBuilder.

    String one = "abc";
    String two = "xyz";
    String three = one + two;
    

    This will compile String three as:

    String three = new StringBuilder().append(one).append(two).toString();
    

    This is quite helpful and saves us some runtime. However this process is not always optimal. Take for example:

    String out = "";
    for( int i = 0; i < 10000 ; i++ ) {
        out = out + i;
    }
    return out;
    

    If we compile to bytecode and then decompile the bytecode generated we get something like:

    String out = "";
    for( int i = 0; i < 10000; i++ ) {
        out = new StringBuilder().append(out).append(i).toString();
    }
    return out;
    

    The compiler has optimised the inner loop but certainly has not made the best possible optimisations. To improve our code we could use:

    StringBuilder out = new StringBuilder();
    for( int i = 0 ; i < 10000; i++ ) {
        out.append(i);
    }
    return out.toString();
    

    Now this is more optimal than the compiler generated code, so there is definitely a need to write code using the StringBuilder/StringBuffer classes in cases where efficient code is needed. The current compilers are not great at dealing concatenating strings in loops, however this could change in the future.

    You need to look carefully to see where you need to manually apply StringBuilder and try to use it where it will not reduce readability of your code too.

    Note: I compiled code using JDK 1.6, and and decompiled the code using the javap program, which spits out byte code. It is fairly easy to interpret and is often a useful reference to look at when trying to optimise code. The compiler does change you code behind the scenes so it is always interesting to see what it does!

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

Sidebar

Related Questions

The answer to this SO question says that you can create an api by
This answer says that Linq is targeted at a slightly different group of developers
I'm supposed to answer this question. After some research it says that add and
In this answer https://stackoverflow.com/a/8649429/1497 Eric Lippert says that FYI we are highly likely to
In this answer to a question I asked. Kathy Van Stone says that adding
This answer says that the XmlHttpRequestObject Level 2 supports cross-site ajax calls. I know
This answer on another question says that array.map(&:to_s) is faster than array.map { |n|
This answer says: Vim's undo/redo system is unbeatable. Type something, undo, type something else,
The manual does not answer this question : it says the name of the
I want to write a regular expression that will match the following string a

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.