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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T23:10:56+00:00 2026-05-20T23:10:56+00:00

Have you ever thought about the implications of this change in the Java Programming

  • 0

Have you ever thought about the implications of this change in the Java Programming Language?

The String class was conceived as an immutable class (and this decision was intentionally thought-out). But String concatenation is really slow, I’ve benchmarked it myself. So the StringBuffer was born. Really great class, synchronized and really fast. But some people were not happy with the performance cost of some synchronized blocks, and the StringBuilder was introduced.

But, when using String to concatenate not too many objects, the immutability of the class makes it a really natural way to achieve thread-safety. I can understand the use of StringBuffer when we want to manage several Strings. But, here is my first question:

  1. If you have, say, 10 or fewer strings that you want to append, for example, would you trade simplicity for just some milliseconds in execution time?

    I’ve benchmarked StringBuilder too. It is more efficient than StringBuffer (just a 10% improvement). But, if in your single-threaded program you’re using StringBuilder, what happens if you sometimes want to change the design to use several threads? You have to change every instance of StringBuilder, and if you forget one, you’ll have some weird effect (given the race condition that may arise) that can be produced.

  2. In this situation, would you trade performance for hours of debugging?

Ok, that’s all. Beyond the simple question (StringBuffer is more efficient than “+” and thread-safe, and StringBuilder is faster than StringBuffer but no thread-safe) I would like to know when to use them.

(Important: I know the differences between them; this is a question related to the architecture of the platform and some design decisions.)

  • 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-20T23:10:57+00:00Added an answer on May 20, 2026 at 11:10 pm

    Nowadays both StringBuffer and Builder are sort of useless (from performance point of view).
    I explain why:

    StringBuilder was supposed to be faster than StringBuffer but any sane JVM can optimize away the synchronization. So it was quite a huge miss (and small hit) when it was introduced.

    StringBuffer used NOT to copy the char[] when creating the String (in non shared variant); however that was a major source of issues, incl leaking huge char[] for small Strings. In 1.5 they decided that a copy of the char[] must occur every time and that practically made StringBuffer useless (the sync was there to ensure no thread games can trick out the String). That conserves memory, though and ultimately helps the GC (beside the obviously reduced footprint), usually the char[] is the top3 of the objects consuming memory.

    String.concat was and still is the fastest way to concatenate 2 strings (and 2 only… or possibly 3). Keep that in mind, it does not perform an extra copy of the char[].

    Back to the useless part, now any 3rd party code can achieve the same performance as StringBuilder. Even in java1.1 I used to have a class name AsycnStringBuffer which did exactly the same what StringBuilder does now, but still it allocates larger char[] than StringBuilder. Both StrinBuffer/StringBuilder are optimized for small Strings by default you can see the c-tor

      StringBuilder(String str) {
        super(str.length() + 16);
        append(str);
        }
    

    Thus if the 2nd string is longer than 16chars, it gets another copy of the underlying
    char[]. Pretty uncool.

    That can be a side effect of attempt at fitting both StringBuilder/Buffer and the char[] into the same cache line (on x86) on 32bit OS… but I don’t know for sure.

    As for the remark of hours of debugging, etc. Use your judgment, I personally do not recall ever having any issues w/ strings operations, aside impl. rope alike structure for the sql generator of JDO impl.


    Edit:
    Below I illustrate what java designers didn’t do to make String operations faster.
    Please, note that the class is intended for java.lang package and it can put there only by adding it to the bootstrap classpath. However, even if not put there (the difference is a single line of code!), it’d be still faster than StringBuilder, shocking? The class would have made string1+string2+… a lot better than using StringBuilder, but well…

    package java.lang;
    
    public class FastConcat {
    
        public static String concat(String s1, String s2){
            s1=String.valueOf(s1);//null checks
            s2=String.valueOf(s2);
    
            return s1.concat(s2);
        }
    
        public static String concat(String s1, String s2, String s3){
            s1=String.valueOf(s1);//null checks
            s2=String.valueOf(s2);
            s3=String.valueOf(s3);
            int len = s1.length()+s2.length()+s3.length();
            char[] c = new char[len];
            int idx=0;
            idx = copy(s1, c, idx);
            idx = copy(s2, c, idx);
            idx = copy(s3, c, idx);
            return newString(c);
        }
        public static String concat(String s1, String s2, String s3, String s4){
            s1=String.valueOf(s1);//null checks
            s2=String.valueOf(s2);
            s3=String.valueOf(s3);
            s4=String.valueOf(s4);
    
            int len = s1.length()+s2.length()+s3.length()+s4.length();
            char[] c = new char[len];
            int idx=0;
            idx = copy(s1, c, idx);
            idx = copy(s2, c, idx);
            idx = copy(s3, c, idx);
            idx = copy(s4, c, idx);
            return newString(c);
    
        }
        private static int copy(String s, char[] c, int idx){
            s.getChars(c, idx);
            return idx+s.length();
    
        }
        private static String newString(char[] c){
            return new String(0, c.length, c);
            //return String.copyValueOf(c);//if not in java.lang
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Have you ever tried this before? static void Main(string[] args) { int x =
When have you ever directly applied the concepts of dynamic programming to solve a
When have you ever personally come upon the halting problem in the field? This
I have looked and thought about good and bad points of my past designs
I've been thinking about doing my own language (practicality: it's a thought experiment). One
What is the most evil or dangerous code fragment you have ever seen in
Have you ever seen any of there error messages? -- SQL Server 2000 Could
Have you ever worked on a (full-time) project where using Agile methodologies actually allowed
Have you ever had alternating background colors in a Jasper report and then exported
have you ever noticed how in gmail, as the emails are updated in your

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.