Meaningless Drivel is fun!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes number of object ... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "number of object ..." Watch "number of object ..." New topic
Author

number of object ...

ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
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

Joined: Dec 29, 2004
Posts: 1071
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

Joined: Jul 23, 2003
Posts: 83
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: 3830
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

Joined: Aug 27, 2004
Posts: 411
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: number of object ...
 
Similar Threads
Count the number of object
String literal Pool doubt
BJO - String literal pool contains object ???
Doubt relating to String
String literal pool