• 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

Garbage collect with String Objects

 
Greenhorn
Posts: 7
MyEclipse IDE Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Ranch Hand
Posts: 64
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Core Java please check your private messages for some administrative matter...
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
On the real exam GC questions NEVER use objects of type String.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic