This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Garbage collect with String Objects Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "Garbage collect with String Objects" Watch "Garbage collect with String Objects" New topic
Author

Garbage collect with String Objects

P Malhotra
Greenhorn

Joined: Jul 27, 2011
Posts: 7


Hello everyone

I am using string and I know that we can create string objects by two different ways:

1) String s="Hello"; //Line1
2) String s1= new String("Hello"); //Line2

so if we are using the code written in Line1 that means we are creating a String object in the String literal pool
and
the Line2 will create an String object in Heap

So what is String literal pool ?
Is it like heap or it is a table where we can store actual objects reference.

and

when will garbage collect remove the String objects from the heap


Thanks

Naveen Madarapu
Ranch Hand

Joined: May 24, 2011
Posts: 63

Remember objects are always created on heap only. //created in line1 and line2
String pool is an constant memory location which is store the literal objects reference.


OCPJP
P Malhotra
Greenhorn

Joined: Jul 27, 2011
Posts: 7



Thanks for your reply

and what about the garbage collect if pool is holding the address of the object than when will garbage collect remove the Objects.
Prabhjot Jassal
Greenhorn

Joined: Jul 19, 2010
Posts: 22
You have 2 objects on the heap (i am not too sure about the string pool ..so there might be only 1 object in heap)

The objects are eligible to remove from the memory when they are no longer referenced. Eg. if i say s = null, then there will be no way to get "Hello" object on the heap (assuming there are 2 objects on the heap)

Could please someone clarify how many objects would be in the heap? Thanks
Ankit Garg
Saloon Keeper

Joined: Aug 03, 2008
Posts: 9189
    
    2

Core Java please check your private messages for some administrative matter...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8712
On the real exam GC questions NEVER use objects of type String.


Eliminate fossil fuel subsidies. (If you're not on the edge, you're taking up too much room.)
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Garbage collect with String Objects
 
Similar Threads
String Handling
Problem with String and StringXxx objects
String Objects
String literal Pool doubt
String is created on pool?