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 6611787
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:00:56+00:00 2026-05-25T20:00:56+00:00

According to Netbeans hint named Use chain of .append methods instead of string concatenation

  • 0

According to Netbeans hint named Use chain of .append methods instead of string concatenation

Looks for string concatenation in the parameter of an invocation of the append method of StringBuilder or StringBuffer.

Is StringBuilder.append() really more efficient than strings concatenation?

Code sample

StringBuilder sb = new StringBuilder();
sb.append(filename + "/");

vs.

StringBuilder sb = new StringBuilder();
sb.append(filename).append("/");
  • 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-25T20:00:56+00:00Added an answer on May 25, 2026 at 8:00 pm

    You have to balance readability with functionality.

    Let’s say you have the following:

    String str = "foo";
    str += "bar";
    if(baz) str += "baz";
    

    This will create 2 string builders (where you only need 1, really) plus an additional string object for the interim. You would be more efficient if you went:

    StringBuilder strBuilder = new StringBuilder("foo");
    strBuilder.append("bar");
    if(baz) strBuilder.append("baz");
    String str = strBuilder.toString();
    

    But as a matter of style, I think the first one looks just fine. The performance benefit of a single object creation seems very minimal to me. Now, if instead of 3 strings, you had 10, or 20, or 100, I would say the performance outweighs the style. If it was in a loop, for sure I’d use the string builder, but I think just a couple strings is fine to do the ‘sloppy’ way to make the code look cleaner. But… this has a very dangerous trap lurking in it! Read on below (pause to build suspense… dun dun dunnnn)

    There are those who say to always use the explicit string builder. One rationale is that your code will continue to grow, and it will usually do so in the same manner as it is already (i.e. they won’t take the time to refactor). So you end up with those 10 or 20 statements, each creating their own builder when you don’t need to. So to prevent this from the start, they say always use an explicit builder.

    So while in your example, it’s not going to be particularly faster, when someone in the future decides they want a file extension on the end, or something like that, if they continue to use string concatenation instead of a StringBuilder, they’re going to run into performance problems eventually.

    We also need to think about the future. Let’s say you were making Java code back in JDK 1.1 and you had the following method:

    public String concat(String s1, String s2, String s3) {
        return s1 + s2 + s3;
    }
    

    At that time, it would have been slow because StringBuilder didn’t exist.

    Then, in JDK 1.3, you decided to make it faster by using StringBuffer (StringBuilder still doesn’t exist yet). You do this:

    public String concat(String s1, String s2, String s3) {
        StringBuffer sb = new StringBuffer();
        sb.append(s1);
        sb.append(s2);
        sb.append(s3);
        return sb.toString();
    }
    

    It gets a lot faster. Awesome!

    Now JDK 1.5 comes out, and with it comes StringBuilder (which is faster than StringBuffer) and the automatic translation of

    return s1 + s2 + s3;
    

    to

    return new StringBuilder().append(s1).append(s2).append(s3).toString();
    

    But you don’t get this performance benefit because you’re using StringBuffer explicitly. So by being smart, you have caused a performance hit when Java got smarter than you. So you have to keep in mind that there are things out there you won’t think of.

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

Sidebar

Related Questions

I've found few pages about Netbeans and date of CSS3 implementation. According to them,
I installed Netbeans IDE 6.8 today to use it while learning Ruby. Here the
In order to use Java FX in Netbeans I installed java jdk 7 by
I have installed and configured NetBeans 6.7 for c++ according to the official manual:
according to netbeans documentation to check out from svn: Checking out Files from a
i have installed minGW on my PC according to http://netbeans.org/community/releases/72/cpp-setup-instructions.html , and i have
According to a poll here on StackOverflow , NetBeans is the best PHP IDE
According to some textbooks, the compiler will use sub* to allocate memory for local
According to my previous question integration of jsf2.0 and spring 3.1 and hibernate 4.1
According to the C++11 standard, is the following program well-formed and portable C++? int

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.