File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes creating new Strings(from K&B) 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 "creating new Strings(from K&B)" Watch "creating new Strings(from K&B)" New topic
Author

creating new Strings(from K&B)

Angela lewis
Ranch Hand

Joined: Mar 01, 2004
Posts: 100





If statement //2 it says 2 String objects are created. Does it mean 1 in the heap and one in the pool (is it correct to call the literal placed in the pool as object or is it called something else).

Secondly, if //2 is executed after //1 then will //2 still create 2 objects or will it create 1 object n the heap and as far as pool is concerned it'll refer to existing "abc" that's in the pool from //1

please clarify
Angela lewis
Ranch Hand

Joined: Mar 01, 2004
Posts: 100
A small addition to above question.
If the exam question asks how many objects are created, then should the one in the pool be also counted or only the one in the heap .
[ June 11, 2004: Message edited by: Angela lewis ]
Sundar Murthi
Ranch Hand

Joined: Mar 05, 2004
Posts: 209

If statement //2 it says 2 String objects are created. Does it mean 1 in the heap and one in the pool


actuly 2 String objects are not created. Only one string object is created and another one is a string literal which is not considered as object and also it is nor Garbage collectable

am i correct?
Angela lewis
Ranch Hand

Joined: Mar 01, 2004
Posts: 100
Thanks for replying
actuly 2 String objects are not created. Only one string object is created and another one is a string literal which is not considered as object and also it is nor Garbage collectable



But the book K&B (page 360) says

Sundar Murthi
Ranch Hand

Joined: Mar 05, 2004
Posts: 209
Hi Angela

I can't find your specified one on that page.
The book version may be varied.

But i studied in K&B at the time of crating String object using new Operator one non pole string object in memory and one string litral is created in string pool memory. The object reffering to that string.



>>Any other ranchers can throw some light on this.
Angela lewis
Ranch Hand

Joined: Mar 01, 2004
Posts: 100
If the question is, how many objects are created

What should i write 1 or 2.
Sundar Murthi
Ranch Hand

Joined: Mar 05, 2004
Posts: 209
Hi Angela

If i am in the exam my sellection is 1 .
Because literals are not considered as objects.
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Let me try to clarify this question a little bit.

First of all, don't worry about this question on the exam - it won't be asked. You do not need to understand creation and garbage collection of String literals (or the Srting literal pool, for that matter), for the SCJP exam. You will have to understand object creation and garbage collection, but String literals are a special case and are not included on the exam.

Now, knowing that none of what I am about to say is on the exam, let's get to the guts of this. Take this line:



In this case, two String objects are created and both of them are on the heap. All objects are created on the heap. Always.

So what's the String Literal Pool? Well, it's just a pool of references, not objects. Each of the references in that pool refers to an object on the heap (because that's where all objects are).

With that line of code, you'd have two String objects on the heap with the value "abc". One of those objects would be created when the class is loaded. That object would have a reference to it from the String literal pool. The other object would be created when the above line was executed (based on the keyword "new"). The variable s obtains a reference to that object.

When s goes out of scope, assuming no other variables reference the object referred to by s, that String would be eligible for garbage collection. What about the one referenced from the String Literal Pool? That one is not eligible for garbage collection because you retain a reference to that object from the Pool.

I hope that helps,
Corey


SCJP Tipline, etc.
Sundar Murthi
Ranch Hand

Joined: Mar 05, 2004
Posts: 209
Ok Corey

I understand ur explanation

but consider the following one




At line2 how many objects are created..

waiting for ur reply..
Swamy Nathan
Ranch Hand

Joined: May 16, 2004
Posts: 187
String s = new String("abc");
String s1 = new String("abc"); //line2

Let me make a try. I am hoping I understood Corey's explanation correctly.



One of those objects would be created when the class is loaded. That object would have a reference to it from the String literal pool. I guess that is "abc" literal common to both lines of code.

Then two more objects will be created for each useage of new.

Total objects created is 3 out of these three, two will be pointed(referred) to by the refrences s and s1. One will not be pointed to by any reference in the code and only by a reference from the literal pool. [It looks like the object pointed to by the reference in the literal pool is an extra unused surplus.]

If there was any other String s3 with value of "abc" where new was not used it would point to the same object pointed to by the refrence in literal pool.

From Corey's explanation a literal pool is like (well not exactly) the free lunch service provided for all students in a school cafeteria and using the new means u choose to buy/bring your own lunch. But for some reason the cafeteria seems to also provide a free lunch version of every personal lunchbox in the hope that some other student may utilize that free lunch.
[ June 15, 2004: Message edited by: Swamy Nathan ]
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Originally posted by Sundar Murthi:
At line2 how many objects are created..


Swamy is correct, there are 3 String objects created after line 2 has executed. Only one String object is created when the class is loaded and that constant is used for both lines. Of course, you then use the keyword "new" twice, which causes two more String objects to be created.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: creating new Strings(from K&B)