This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes number of objects created 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 objects created" Watch "number of objects created" New topic
Author

number of objects created

Naresh Gunda
Ranch Hand

Joined: Oct 15, 2005
Posts: 163
1. StringBuffer s1 = new StringBuffer("abc");
2. StringBuffer s2 = s1;
3. StringBuffer s3 = new StringBuffer("abc");

How many objects are created ?

2 . Please correct me if i am wrong.
Raghusham Sankargal
Ranch Hand

Joined: Oct 10, 2005
Posts: 59
You are right. Only two objects are created. Because s1 and s2 are references pointing to the same memory.


Raghusham Sankargal
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4442
    
    5

Well, only two StringBuffer objects are created. The String object "abc" is the third.


Junilu - [How to Ask Questions] [How to Answer Questions]
Krishna Latha Grandhi
Ranch Hand

Joined: Nov 08, 2005
Posts: 110
String s1= new String("abc");

2 objects are created. one in the heap and another "abc" in the string pool.

I have a doubt that if we assign null to s1 (s1=null then object created

in the string pool is available for garbage collection or not.

Please give the detailed explanation ....

Thanks in advance

Regards,
Krishna.
Raghusham Sankargal
Ranch Hand

Joined: Oct 10, 2005
Posts: 59
Here's some information to you hari, The object gc takes the object only when there are no references to it(internal or external). You cannot be sure when gc will be called and when it acts. This is what I think if I am not mistaken. Other ranchers please correct me if I am wrong.
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
Originally posted by Hari Krishna:
String s1= new String("abc");

2 objects are created. one in the heap and another "abc" in the string pool.

I have a doubt that if we assign null to s1 (s1=null then object created

in the string pool is available for garbage collection or not.

Please give the detailed explanation ....

Thanks in advance

Regards,
Krishna.


Yes, 2 objects are eligible for garbage collection. But as you might know, when they will get collected, no body knows. It's on JVM. You can only request GC to run but can't force.
raghava yelamkonda
Greenhorn

Joined: Nov 28, 2005
Posts: 10
I think answer should be 3
total 3 objects are created
1 "abc"
2 StringBuffer s1 = new StringBuffer("abc");
3 StringBuffer s3 = new StringBuffer("abc");

can anybody explain what is the correct answer , what are they ?
why people are counting "abc" and only 1 stringbuffer object ... what about the other stringbuffer object
Srinivasa Raghavan
Ranch Hand

Joined: Sep 28, 2004
Posts: 1228
Actually this thread got hijacked from counting total number of objects created to total number of objects GC'd

Totally there are 3 objects ( 2 String buffers ) & a String in the StringPool.When S1 is set to null then only the StringBuffer pointed by s1 is eligible for GC and not the string in the string pool
[ December 15, 2005: Message edited by: Srinivasa Raghavan ]

Thanks & regards, Srini
MCP, SCJP-1.4, NCFM (Financial Markets), Oracle 9i - SQL ( 1Z0-007 ), ITIL Certified
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: number of objects created
 
Similar Threads
How many Objects are created ?
Object creation??
How many objects created?
StringBuffer Question.
how many objects are created?