• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 354
  • Last Modified:

Using StringBuffer and StringBuidler

When I run this code below, the output that comes are

blooperwhopper
bloopershopper

But I was expecting:  blooperwhopper blooperwhoppershopper
I am confused why "whopper" is not included between blooper and shopper (second output).

public class TestClass {
  public static void main(String[] args) {

    StringBuffer s = new StringBuffer("blooper");
    StringBuilder sb = new StringBuilder(s);
    s.append("whopper");
    sb.append("shopper");
	
    System.out.println(s);
    System.out.println(sb);
  }
}

Open in new window


Could you please provide me clear and detail explanation why "whopper" is not included in the second output?
0
techbro
Asked:
techbro
3 Solutions
 
objectsCommented:
'whopper' is appended *after* sb has been initialised with the value of s (ie. it taks a copy at line 5)
0
 
for_yanCommented:
because when

 StringBuilder sb = new StringBuilder(s);


s was still  "blooper"

0
 
objectsCommented:
   StringBuffer s = new StringBuffer("blooper");

s -> blooper

    StringBuilder sb = new StringBuilder(s);

s -> blooper
sb -> blooper

    s.append("whopper");

s -> blooperwhopper
sb -> blooper

    sb.append("shopper");

s -> blooperwhopper
sb -> bloopershopper
      
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
for_yanCommented:
and when you then append "whooper" sb is another object altogether - it cannot hchange because
you cahnge the object which was used for its creatinion
0
 
objectsCommented:
> and when you then append "whooper" sb is another object altogether - it cannot hchange because
> you cahnge the object which was used for its creatinion

thats incorrect. The object used for its creation is irrelevant. StringBuild copies the characters, it does not reference the String used to create it
0
 
for_yanCommented:
So waht I wrote is correct, sb is another aobject altogether and it cannot change becauye abother
object whuch was used for its creation was now changed. It is relevent, vbecause that is exactly form where comes the confusion.
0
 
objectsCommented:
see my earlier comment, it explains what is going on
0
 
lexlythiusCommented:
@objects is right.
@for_yan is probably also right but explains it in a confusing way.

All three classes String, StringBuffer and StringBuilder are wrappers for arrays of characters, char[]. (Also, all of them implement the CharSequence interface).

String is immutable: once initialized, it cannot change. You can only create new objects that internally copy the String's underlying character array.

Which is exactly how StringBuffer works to provide for a mutable sequence of characters. When initialized with a String (or any other CharSequence), StringBuffer takes its underlying char[], copies it (i.e., there are now two separate char arrays in different memory locations and owned by different objects--they just happen to hold the same characters) and keeps that copy to be modified later on.

StringBuilder is analogous to StringBuffer, only more performant since it is unsynchronized.

In your code, the key is line 5 as @objects pointed out.
StringBuilder sb = new StringBuilder(s);

Open in new window

You've just initialized a StringBuilder by the name of sb, using the same characters wrapped at that time by a StringBuffer by the name of s. That's were their paths split. Whatever you do to any of them thereafter, they will remain independent objects that just may happen to have some or all of their characters in common.
0
 
techbroAuthor Commented:
Thank you for your response.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now