*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes About StringBuffer and String Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "About StringBuffer and String" Watch "About StringBuffer and String" New topic
Author

About StringBuffer and String

Sam Sunamin
Ranch Hand

Joined: Mar 16, 2007
Posts: 113
StringBuffer sb = new StringBuffer("abc");
sb.append("def");
System.out.println("sb = " + sb); // output is "sb = abcdef"

The above code used StringBuffer.

My Question is if the compiler creates "abc" and "def" string in the pool? If yes. What's the benefits of StringBuffer? It also leaves a lot abandoned String objects in the memory.

The following the explaination from another part of book SCJP.Sun.Certified.Programmer.for.Java.5.Study.Guide.Exam.310-055.Dec.2005

String s1 = "spring ";
String s2 = s1 + "summer ";
s1.concat("fall ") ;
s2.concat(s1);
s1 += "winter ";
System.out.println(s1 + " " + s2);
What is the output? For extra credit, how many String objects and how many reference variables were created prior to the println statement?
Answer: The result of this code fragment is "spring winter spring summer". There are two reference variables, s1 and s2.

There were a total of eight String objects created as follows: "spring", "summer " (lost), "spring summer", "fall" (lost), "spring fall" (lost), "spring summer spring" (lost), "winter" (lost), "spring winter" (at this point "spring" is lost). Only two of the eight String objects are not lost in this process.


Yours Sam<br />SCJP5.0 97%<br />SCBCD5.0 72%
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10911
    
  12

1) Please do not post the same questtion twice.

2) Your name does not meet our Naming Policy. Please read it, and follow the link at the end to correct your screen name.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Sam Sunamin
Ranch Hand

Joined: Mar 16, 2007
Posts: 113
1) I do not mean to post twice. I just forgot to add some information on my the first post.
2)I have changed my Screen name.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10911
    
  12

You can always edit your posts. at the top of each you'll see a little paper/pencil icon. this allows you to edit your posts. You can even delete your post there. Note that if yours is the first post of the thread and you delete it, the entire thread goes away.

Thanks for changing your name.

Now...

yes, "def" and "abc" will be added to the string pool. EVERY string literal is put in the pool, and you don't have control over this.

Using a StringBuffer actually causes FEWER strings to be abandoned in the heap. A String is immutable. Once it is created, it can never be changed. so when you concatenate Strings, new Strings are created all over the place. each time you use the "+" operator, it's a new String, so

stringone + stringtwo + stringthree + string4;

creates (i believe) 3 strings - two temporary ones that are lost almost as soon as they are created, and one we keep. That is in addition to the four strings we have already created.

The code s1 += "winter" doesn't change the String s1 points to, but creates a brand new string and re-assigns the reference to that new string.

a StringBuffer would change the underlying object itself, thus not causing the object to be lost.
[ March 23, 2007: Message edited by: Fred Rosenberger ]
Sam Sunamin
Ranch Hand

Joined: Mar 16, 2007
Posts: 113
Thank you Fred for you reply and I deleted my duplicated post.

Is there anyway that we can see how many String objects are created in the memory? So that I can see what's happening in my JVM.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: About StringBuffer and String
 
Similar Threads
explain String Object please ?
String declaration
how many String objects are created
string function problem
K&B SCJP Study Guide for Java 5 ERRATA p 419