• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Question on Garbage Collection

 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I guess what you are saying is correct. The String object should be created at compile time.
 
Ranch Hand
Posts: 352
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.)
 
Stephen Davies
Ranch Hand
Posts: 352
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 352
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But an Strings "ABC" not been Objects? if two are equals there are three objects.
I can wrong.
 
Stephen Davies
Ranch Hand
Posts: 352
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ruben, ahhhh! now I get it , doh, I guess I gotta brush up more on my Strings"

Thanks for that

 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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...
 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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

 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic