One object at String Literal Pool : "Hi" Second object at String Literal Pool : " How are you" Third object at String Literal Pool : "Hi How are you" Fourth object at Garbage Collection Heep , that is assign to reference s & have internally reference of "Hi how are you" object .
I hope my points are clear . Please help . thanks a lot .
Originally posted by rathi ji: One object at String Literal Pool : "Hi" Second object at String Literal Pool : " How are you" Third object at String Literal Pool : "Hi How are you" Fourth object at Garbage Collection Heep , that is assign to reference s & have internally reference of "Hi how are you" object .
I believe that String objects are also created in the Heap(the other being stack for method variables and parameters) as with normal objects. And String objects are immutable (the actual string object never change). It doesn't mean that they are not garbage collected if they are eligible.
Now in your code example there are 3 String objects which are created. But after reaching end of , String objects "Hi" and " How are you" are eligible for garbage collection because they were lost in the heap. It should clear some concepts regarding String objects... Please comment
ankur rathi
Ranch Hand
Joined: Oct 11, 2004
Posts: 3829
posted
0
yaah steven , sorry it was a mistake . its only 3 object . and Swapan , object will not be on GCH because they are string literal object . String s = new String("Hi"); In this case , one object is on SLP & one is on GCH . hope you got the point .
I agree with Steven Bell, there are only 3 objects.
Post by Swapan: String objects "Hi" and " How are you" are eligible for garbage collection because they were lost in the heap.
"Hi" and "How are you" are created in String constant pool and these strings are not eligible for GC.
Objects are created on the heap only when you use new operator.
Hope this helps
Cheers,<br />Jay<br /> <br />(SCJP 1.4)<br />Heights of great men were not achieved in one day, they were toiling day and night while their companions slept.