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

The Archive Base Latest Questions

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

I have a feeling that using the: StringBuilder.toString() is slow and very resource-consuming.. So

  • 0

I have a feeling that using the: StringBuilder.toString() is slow and very resource-consuming..

So I’m thinking about something like this:

public static void doSomething(String data){ ... }

public static void main(String[] args)
{
    StringBuilder s = new StringBuilder();
    doSomething(""+s);
}

But I want to know if there is an other “better and fast” way than this, because doSomething(""+s) in a loop will make a new instance of String because of the empty quotes “” I think, and it’s not a good idea to put this inside a loop.

  • 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:16:35+00:00Added an answer on May 25, 2026 at 8:16 pm

    doSomething(""+s); gets translated to the following code by the JVM

    doSomething( new StringBuilder().append("").append(s.toString() ).toString() );

    So now, instead of having 1 string builder you have 2, and called StringBuilder.toString() twice.

    The better and faster way is to use just StringBuilder, without concatenating string manually.


    I just checked the bytecode generated with java 1.6.0_26 and the compiler is intelligent and calls toString() only once, but it still creates 2 instances of StringBuilder. Here’s the byte code

     0  new java.lang.StringBuilder [16]
     3  dup
     4  invokespecial java.lang.StringBuilder() [18]
     7  astore_1 [s]
     8  new java.lang.StringBuilder [16]
    11  dup
    12  invokespecial java.lang.StringBuilder() [18]
    15  aload_1 [s]
    16  invokevirtual java.lang.StringBuilder.append(java.lang.Object) : java.lang.StringBuilder [19]
    19  invokevirtual java.lang.StringBuilder.toString() : java.lang.String [23]
    22  invokestatic my.test.Main.doSomething(java.lang.String) : void [27]
    25  return 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a feeling that this question is very simple, but I just can't
I have a strong gut feeling that using SharePoint RunWithElevatedPrivileges should be avoided like
I have the feeling that using joins could make this cleaner public override string[]
I have a feeling that this probably is not possible using strictly CSS, but,
This is my first post and I have the feeling that this is something
I have a feeling that I already know the answer to this one, but
I have a feeling that this query is pretty easy to construct, I just
I somehow have the feeling that modern systems, including runtime libraries, this exception handler
This is an SDL problem, however I have the strong feeling that the problem
I have a feeling that Apache is using a different php.ini file that the

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.