• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

number of object ...

 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just want to confirm , what I am getting from this ...

String s = "Hi".concat(" How are you");

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 .
 
Steven Bell
Ranch Hand
Posts: 1071
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think there are just the three objects "Hi" " How are you" and "Hi How are you". Then one reference, s, to "Hi How are you"

I'm not sure what you mean by an Object at the Garbage Collection Heap.
 
Swapan Mazumdar
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 .
 
Jay Pawar
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic