Академический Документы
Профессиональный Документы
Культура Документы
Java FAQ
Recent Topics
Register / Login A friendly place for programming This week's giveaway is in the Jobs Discussion forum. greenhorns! We're giving away four copies of Presenting for Geeks
and have Dirk Haun on-line! See this thread for details.
Author
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.
www.coderanch.com/t/262081/java-programmer-SCJP/certification/StringBuffer-String
1/3
7/16/13
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 />SC JP5.0 97%<br />SC BC D5.0 72% fred rosenberger lowercase baba Bartender Joined: Oct 02, 2003 Posts: 10137
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.
8
I like...
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: 10137
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.
8 Now...
I like...
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
www.coderanch.com/t/262081/java-programmer-SCJP/certification/StringBuffer-String 2/3
7/16/13
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.
Similar Threads K&B SCJP Study Guide for Java 5 ERRATA p 419 explain String Object please ? String declaration string function problem how many String objects are created
All times above are in your local time zone & format.T he current ranch time (not your local time) is Jul 16, 2013 07:28:34 .
www.coderanch.com/t/262081/java-programmer-SCJP/certification/StringBuffer-String
3/3