Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

String Pool and StringBuilder  RSS feed

 
L Hasan
Greenhorn
Posts: 24
4
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'm reading through the OCA certification book and am on the section regarding Strings. There's an example of not using a StringBuilder:



It states that every time you go through the loop, the alpha reference is assigned a new String object, and the old one becomes eligible for garbage collection. e.g. first loop alpha = "", second alpha = "a" and "" is for GC.

However, from what I've read of the String Pool, it would seem that each literal created in this way would be placed into the Pool. So wouldn't that mean said literals don't become eligible for GC because the pool still holds reference to them?

Thanks,

L.
 
Ganesh Patekar
Bartender
Posts: 696
23
Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can see only one String literal "" which is created on class loading, whose reference will be in String literal pool although no String reference variable referring to this String object so I think only this String will not be eligible for GC.

At runtime rest of the String objects in loop are created because of String concatination which uses StringBuilder to append these Strings then converts appended StringBuilder to String using toString method of StringBuilder which creates new String object (which is not String literal) using new operator so from "a" String object will be eligible for GC and so on.
 
Ganesh Patekar
Bartender
Posts: 696
23
Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And you missed assignment operator (=) in for Statements's init part.

Always mention details of your sources like which book you are referring to? --->QuoteYourSources
 
L Hasan
Greenhorn
Posts: 24
4
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The missing = on the loop init is my bad. This information comes from the OCA Java SE 8 Programmer 1 Certification book by Boyarsky and Seikoff.

How do you know that a String Builder is used at runtime? Quoted from the book page 111: However, because the String object is immutable, a new String is assigned to alpha and the "" object becomes eligible for garbage collection."

It then goes on to state that proceeding strings created to replace alpha, like "a", "ab" etc. will all also become eligible for GC after each loop.

Thanks for the response,

L.
 
Ganesh Patekar
Bartender
Posts: 696
23
Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
L Hasan wrote:How do you know that a String Builder is used at runtime?


When we compile above example and disassemble it with the command: javap -c Test.class then we get below:

where at #3 creates StringBuilder object and at both #5 appends these Strings then at #7 converts StringBuilder to String.

In general coding It would look like below code:

I'm not convinced only about String literal "" being eligible for GC, yes the remaining Strings which gets created in loop like "a", "ab" etc. are eligible for GC.
Worth reading --->Strings, Literally by Corey McGlone

For OCAJP exam you don't need to know how internally It is implemented.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!