• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Garbage collect with String Objects

 
P Malhotra
Greenhorn
Posts: 7
MyEclipse IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 64
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
P Malhotra
Greenhorn
Posts: 7
MyEclipse IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 9509
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Core Java please check your private messages for some administrative matter...
 
Bert Bates
author
Sheriff
Posts: 8898
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On the real exam GC questions NEVER use objects of type String.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic