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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T00:24:52+00:00 2026-05-31T00:24:52+00:00

I was doing tests with the Java StringBuilder, especially the replace(int, int, String) function

  • 0

I was doing tests with the Java StringBuilder, especially the replace(int, int, String) function which is implemented in the AbstractStringBuilder class as followed:

public AbstractStringBuilder replace(int start, int end, String str) {
    if (start < 0)
        throw new StringIndexOutOfBoundsException(start);
    if (start > count)
        throw new StringIndexOutOfBoundsException("start > length()");
    if (start > end)
        throw new StringIndexOutOfBoundsException("start > end");

    if (end > count)
       end = count;
    int len = str.length();
    int newCount = count + len - (end - start);
    if (newCount > value.length)
       expandCapacity(newCount);

    System.arraycopy(value, end, value, start + len, count - end);
    str.getChars(value, start);
    count = newCount;
    return this;
}

The Arraycopy function call does “move” parts the content of the value character array to make space for the later injected str content (str.getChars(value, start)). From my point of view, one should only do this arraycopy if the str length does not match the space to be overwritten in the character array anyways.

Clearly one is very desperate to consider this a performance issue, though making tests with bigger character arrays (>500k chars) and arraycopy in a StringBuilder replacement class leaded to measurable performance improvement.

Tested with java 6 on a Windows 32bit Platform an the same StringBuilder instance for some million replace calls.

Would you consider this to be unimportant, a bug or am I missing something completely?

  • 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-31T00:24:54+00:00Added an answer on May 31, 2026 at 12:24 am

    I would pass it as a request for enhancement.

    Something like

    if (end != start + len)
        System.arraycopy(value, end, value, start + len, count - end);
    

    A further enhancement would be to change array copy.

    public static void arraycopy(Object src,  int  srcPos,
                                        Object dest, int destPos,
                                        int length) {
        if (srcPos != destPos && length != 0)
           arraycopy0(src, srcPos, dest, destPos, length);
    

    }

    private static native void arraycopy0(Object src,  int  srcPos,
                                        Object dest, int destPos,
                                        int length);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Currently I'm doing some unit tests which are executed from bash. Unit tests are
I was doing some tests, where I inserted some records of java bigDecimal to
I was doing one of these online Java tests and I was asked this
I am doing some tests with escape analysis in Java 7 in order to
When you are doing integration tests with either just your data access layer or
I'm doing some tests with nhibernate and I'm modifying batch_size to get bulk inserts.
I'm doing some tests on a Windows Server 2008. When I try to access
I'm doing some tests with new features of CSS3, but this combination only works
I'm doing performance tests on my app engine app. I'm doing it on the
I'm doing some tests, but I see no difference when I use or not

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.