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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T11:31:44+00:00 2026-06-09T11:31:44+00:00

I was looking at the Java reflection classes and noticed this piece of code.

  • 0

I was looking at the Java reflection classes and noticed this piece of code. Made me wonder, why Java uses StringBuffer when StringBuilder is faster?

Wouldn’t Java want to use the fastest implementation, or is there some other reason?

The code is in the Field.class:

static String getTypeName(Class<?> type) {
    if (type.isArray()) {
        try {
            Class<?> cl = type;
            int dimensions = 0;
            while (cl.isArray()) {
                dimensions++;
                cl = cl.getComponentType();
            }
            StringBuffer sb = new StringBuffer();
            sb.append(cl.getName());
            for (int i = 0; i < dimensions; i++) {
                sb.append("[]");
            }
            return sb.toString();
        } catch (Throwable e) { /*FALLTHRU*/ }
    }
    return type.getName();
}
  • 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-09T11:31:45+00:00Added an answer on June 9, 2026 at 11:31 am

    The main reason is that the JVM has advanced techniques to see if can avoid having to do all sorts of things are they are implied by the context. Since the StringBuffer is a local variable that never escapes the method the JVM can safely avoid having to try and acquire a lock on the object before entering the StringBuffer’s synchronised methods — since no other thread will ever be able to call the methods of this particular instance of StringBuffer.

    A quick micro benchmark bears this out. Making buffer a field will slow down the following code by 50%.

    private void doTest(String toCopy) {
        StringBuffer buffer = new StringBuffer();
        for (int i = 0; i < toCopy.length(); i++) {
            buffer.append(toCopy.charAt(i));
        }
        buffer.toString();
    }
    

    With a million length string and 1000 repetitions the above code runs in 8 seconds on my machine. However, once buffer is made into a field rather than a local variable then it takes about 13 seconds (Since the JVM can no longer easily guarantee that buffer will only be accessed by one thread).

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

Sidebar

Related Questions

I'm looking for a converter to make a reflection-style Java code from a normal
I have to refactor and maintain a bunch of horrible similar looking Java classes.
I'm looking for java code (or a library) that calculates the earth mover's distance
I need a Max-Priority Queue data structure. Looking in Java's Priority Queue I noticed
I am looking for a way to transform some classes from Java to .Net
I've been looking into Java reflections. This is an example from Wikipedia http://en.wikipedia.org/wiki/Reflection_(computer_programming ):
I'm looking for Java code that can be used to generate sound at runtime
I'm looking for Java rules engine options that will allow me to use the
I'm looking for Java bindings for XLib. Specially, I need to be able to
I'm looking for Java's equivalent of PHP's isset() ; int board[][]=new int[8][8]; ... if(isset(board[y][x]))

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.