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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T22:04:49+00:00 2026-06-08T22:04:49+00:00

Below is code snippet in instance method String x = new StringBuffer().append(a).append(b).append(c).toString() i am

  • 0

Below is code snippet in instance method

 String x = new StringBuffer().append("a").append("b").append("c").toString()

i am under impression , first new stringbuffer is created, then a is appended atlast of string buffer,
similarly b and c. After that stringbuffer is converted to string. So as per me 2 objects are created(one for string buffer and another for string).
correct? Basically as per me no intermediate objects will be created for String “a”,”b”,”c”. Is this right?

Edit:- as per all of the replies, looks like objects will be created for string literals “a”,”b”,”c” But if i go by link http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/StringBuffer.html#toString(), this should not create temporary strings. Search “Overall, this avoids creating many temporary strings.” on this link. Agreed it is for 1.4.2 but i hope fundamental remain same for 1.6

Yes if i do below instead of above five objects will be created. three for “a”,”b”,”c” . one for string buffer. Then at last for string converted from
stringbuffer. objects for “a”,”b”,”c” and lastly string “abc” will go too pool and be there in for life time

String str1="a";
String str2="b";
String str3="c";
String x = new StringBuffer().append(str1).append(str2).append(str3).toString()

Is above understanding correct?

  • 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-08T22:04:51+00:00Added an answer on June 8, 2026 at 10:04 pm

    There is no difference between your first and second snippet in terms of how many objects are created. Strings "a", "b", and "c" will participate in the process, although their interned copies may be used. In the absence of further references to str1..str3, the compiler is free to transform your second snippet into your first one, eliminating the variables.

    In addition, there may be an internal reallocation inside StringBuffer‘s append, if the memory in its internal string is insufficient to hold the data being appended. This is only a theoretical possibility, but it is there.

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

Sidebar

Related Questions

I have below code snippet SimpleDateFormat dateFormat = new SimpleDateFormat( yyyy-MM-dd hh:mm:ss.SSS); String processedContentDate=2012-04-10
I am very new to asp classic application. The below code snippet is from
With the below snippet I want to append/prepend a string to a list that
I am using the below code snippet to get the html string of a
Which code snippet will give better performance? The below code segments were written in
I am using the below code snippet to FTP simple text files from a
Please look at the below code snippet and let me know how the out
Below is a code snippet, where we retrieve a form value. Before further processing
In the code snippet below, from my jQuery setup, I need to check if
Below is a code snippet from a book. Why can serialNumber still be set

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.