• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

About StringBuffer and String

 
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sam Sunamin
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 113
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Aaaaaand ... we're on the march. Stylin. Get with it tiny ad.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic