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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T09:35:34+00:00 2026-06-03T09:35:34+00:00

I was having a discussion about usage of String s and StringBuffer s in

  • 0

I was having a discussion about usage of Strings and StringBuffers in Java. How many objects are created in each of these two examples?

Ex 1:

String s = "a";
s = s + "b";
s = s + "c";        

Ex 2:

StringBuilder sb = new StringBuilder("a");
sb.append("b");
sb.append("c");

In my opinion, Ex 1 will create 5 and Ex 2 will create 4 objects.

  • 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-03T09:35:35+00:00Added an answer on June 3, 2026 at 9:35 am

    You can determine the answer by analyzing the java bytecode (use javap -c). Example 1 creates two StringBuilder objects (see line #4) and two String objects (see line #7), while example 2 creates one StringBuilder object (see line #2).

    Note that you must also take the char[] objects into account (since arrays are objects in Java). String and StringBuilder objects are both implemented using an underlying char[]. Thus, example 1 creates eight objects and example 2 creates two objects.

    Example 1:

    public static void main(java.lang.String[]);
      Code:
       0:   ldc             #2; //String a
       2:   astore_1
       3:   new             #3; //class java/lang/StringBuilder
       6:   dup
       7:   invokespecial   #4; //Method java/lang/StringBuilder."<init>":()V
       10:  aload_1
       11:  invokevirtual   #5; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
       14:  ldc             #6; //String b
       16:  invokevirtual   #5; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
       19:  invokevirtual   #7; //Method java/lang/StringBuilder.toString:()Ljava/lang/String;
       22:  astore_1
       23:  new             #3; //class java/lang/StringBuilder
       26:  dup
       27:  invokespecial   #4; //Method java/lang/StringBuilder."<init>":()V
       30:  aload_1
       31:  invokevirtual   #5; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
       34:  ldc             #8; //String c
       36:  invokevirtual   #5; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
       39:  invokevirtual   #7; //Method java/lang/StringBuilder.toString:()Ljava/lang/String;
       42:  astore_1
       43:  return   
    }
    

    Example 2:

    public static void main(java.lang.String[]);
      Code:
       0:   new             #2; //class java/lang/StringBuilder
       3:   dup
       4:   ldc             #3; //String a
       6:   invokespecial   #4; //Method java/lang/StringBuilder."<init>":(Ljava/lang/String;)V
       9:   astore_1
       10:  aload_1
       11:  ldc             #5; //String b
       13:  invokevirtual   #6; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
       16:  pop
       17:  aload_1
       18:  ldc             #7; //String c
       20:  invokevirtual   #6; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
       23:  pop
       24:  return  
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We've been having a discussion at work about whether to use Domain Objects in
We are having this discussion in our team about code conventions for Java for:
We are having another discussion here at work about using parametrized sql queries in
Recently I was having a discussion with a friend about Ruby's Proc . You
I was having a discussion with one of my colleagues about how defensive your
I was having a discussion about this with a co-worker and we couldn't come
A friend and I were having a discussion about how a FPS server updates
I was having a discussion earlier about a state machine, and there was a
We are having a discussion about Integration strategies. The main argument is about what
I'm having a discussion about which way to go in a new C++ project.

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.