aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question on Garbage Collection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Question on Garbage Collection" Watch "Question on Garbage Collection" New topic
Author

Question on Garbage Collection

Natalie Ap
Ranch Hand

Joined: Jan 09, 2009
Posts: 49

Hi all,

Please help me understand the solution to the above problem that i got from the following site, question (14)

http://www.examsguide.com/scjp/freequestions2.html

The 1st two objects created are s1 and s3. Is the 3rd object the string "abc"? But it is a string literal. It is known at compile-time. So, when and where is it created?


Question:
1. StringBuffer s1 = new StringBuffer("abc");
2. StringBuffer s2 = s1;
3. StringBuffer s3 = new StringBuffer("abc");
How many objects are created ?


1) 0

2) 1

3) 2

4) 3

Solution: Ans: 3


Fiona
Harshit Rastogi
Ranch Hand

Joined: Apr 15, 2008
Posts: 131
I guess what you are saying is correct. The String object should be created at compile time.


My Techie Blog - Java Questions
Stephen Davies
Ranch Hand

Joined: Jul 23, 2008
Posts: 352
Is it not that when you say StringBuffer s2 = s3, creating a StringBuffer at compile time which points to the value "abc"? Don't forget StringBuffer is an Object, and the String literal rules may differ slightly? (For example StringBuffer is Thread-Safe);

Consider this code and the comments, what are your thoughts?


be a well encapsulated person, don't expose your privates, unless you public void getWife()!
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8883
    
    5
Given the title of this thread, I'm bound to remind you all that on the real exam GC questions will NEVER use objects of type String.

hth,

Bert


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
Stephen Davies wrote:Is it not that when you say StringBuffer s2 = s3, creating a StringBuffer at compile time which points to the value "abc"?

No, it's not creating any object. Remember that s2 is a StringBuffer reference, not an object. In this case we are just assigning the s3 reference to the s2 reference, which means that s2 will refer to the same object as s3 is referring to, or to no object if s3 is not referring to any object (if it is null.)


All code in my posts, unless a source is explicitly mentioned, is my own.
Stephen Davies
Ranch Hand

Joined: Jul 23, 2008
Posts: 352
Ok, point duly taken. However is the given answer of 3 objects incorrect, should there only should be two?

- Admittedly, two was my guess, I was trying to see if there would be a possible reason for 3 objects.

Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
Stephen Davies wrote:Ok, point duly taken. However is the given answer of 3 objects incorrect, should there only should be two?

- Admittedly, two was my guess, I was trying to see if there would be a possible reason for 3 objects.


Hi Stephen,

It depends. If the code is inside a method, and the question asks "How many objects are created inside this method" then the answer is 2, since the method runs at runtime, and the string literal object is created at compile time. If that distinction is not provided, then 3 objects are created in total (compile time and runtime.)

But like Bert said: "Don't worry about Strings and GC for the exam."
Stephen Davies
Ranch Hand

Joined: Jul 23, 2008
Posts: 352
I won't worry about Strings and GC for the exam, I promise.

However, the actual question asked is:



How many objects are created ?


So I cannot see whether they are compile-time or run-time created, so here surely there are only two objects created at line 1. and 3.?
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
Stephen Davies wrote:I won't worry about Strings and GC for the exam, I promise.

However, the actual question asked is:



How many objects are created ?


So I cannot see whether they are compile-time or run-time created, so here surely there are only two objects created at line 1. and 3.?

At runtime there are 2 StringBuffer objects created, you are correct. At compile time, a String object for "abc" will be created, and a reference to this object will be added to the String Constant Pool.
Daniel Faria
Greenhorn

Joined: Dec 25, 2007
Posts: 15
But an Strings "ABC" not been Objects? if two are equals there are three objects.
I can wrong.
Stephen Davies
Ranch Hand

Joined: Jul 23, 2008
Posts: 352
Ruben, ahhhh! now I get it , doh, I guess I gotta brush up more on my Strings"

Thanks for that

Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
Stephen Davies wrote:Ruben, ahhhh! now I get it , doh, I guess I gotta brush up more on my Strings"

Thanks for that


Sure thing, Stephen. Here's a good link for understanding literal strings: String, Literally
That might be more than what you need for the SCJP exam.
Ganeshkumar cheekati
Ranch Hand

Joined: Oct 13, 2008
Posts: 362


one string object is created at compilation time for string "abc" --------object 1

two string buffer objects are created at run time---------object 2,object 3

so 3 objects are created for these 3 statements...


SCJP5 and SCWCD1.5
Think Twice Act Wise...
teja dharma
Ranch Hand

Joined: Feb 07, 2009
Posts: 51
Only two objects are created please try to print s1.hashCode() s3.hashCode() s2.hashCode() you get for s1 , s2 same number and for s3 different number.At the same time a new object is created if and only if we create an object by using new operator.Here in the examples s1's reference is given to s2 but not a fresh object


SCJP 5
Sunny Mattas
Ranch Hand

Joined: Apr 22, 2008
Posts: 45
Only two objects are created


No, I think three objects are created.



At compile time compiler finds string literal "abc", it puts this object in to the string memory pool.This is the first object created.Then Compiler again finds "abc" in line 3 but there is already an "abc" object in string memory pool so it is not put there.This is the use of String memory pool to save the memory. Then at run time two StringBuffer objects are created at line 1 and 3.
Thus there are total of three objects created.

Regards
Sunny mattas
SCJP5


Regards
Sunny Mattas
SCJP5
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question on Garbage Collection