aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes How many String objects have been created? 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 "How many String objects have been created?" Watch "How many String objects have been created?" New topic
Author

How many String objects have been created?

Keith L
Greenhorn

Joined: May 23, 2004
Posts: 24
The line below appears in a question in the Study Guide asking how many objects are created in the code example.

13. String x = new String ("xyz")

The answer to the question gives this explanation:

... Line 13 creates two String objects, one referenced by x and the lost string "xyz"

My initial thought was that the object referenced by x *is* "xyz", ie. that there is only one object created.

I understand that, because of the immutability of Strings, concatenation etc. creates new objects. But in this example, it appears that a single String object is being created and assigned to the reference variable x. why does the answer say that "xyz" is lost? And if "xyz" is lost, what is the "one referenced by x" that the answer talks about; what does x refer to?

Thanks,
Keith
Ken Rubiniac
Greenhorn

Joined: May 14, 2004
Posts: 10
posted May 23, 2004 02:04 PM
--------------------------------------------------------------------------------
The line below appears in a question in the Study Guide asking how many objects are created in the code example.

13. String x = new String ("xyz")

The answer to the question gives this explanation:

... Line 13 creates two String objects, one referenced by x and the lost string "xyz"

My initial thought was that the object referenced by x *is* "xyz", ie. that there is only one object created.

I understand that, because of the immutability of Strings, concatenation etc. creates new objects. But in this example, it appears that a single String object is being created and assigned to the reference variable x. why does the answer say that "xyz" is lost? And if "xyz" is lost, what is the "one referenced by x" that the answer talks about; what does x refer to?

Thanks,
Keith

Hi Kieth,

I came across this issue in the Kathy Sierra and Bert Bates Book , Java 2 Sun Certified Programmer Exam, during my preparation for it:

If you say String s ="abc", This means one string will be created in the string buffer pool.

In your case when you write String p = new("bdf"), "bdf" will be placed in
the normal (non string pool memory) and also in the string pool memory. So
we have two strings. The p reference will refer to the "bdf" in the
non string pool memory. Two strings are created because of the new().

I hope this helps. This is on page 360 of the book mentioned above.
Ken Rubiniac
Greenhorn

Joined: May 14, 2004
Posts: 10
One more though Kieth:

In a sense the "xyz" from the sting buffer pool is lost, but if you
refer to it with String w = "xyz", (a new reference later in the code) the same "xyz" string will be referred to since it is immutable.

Ken
DeyaEddin Muh
Greenhorn

Joined: Dec 18, 2003
Posts: 19

when you write String p = new("bdf"), "bdf" will be placed in
the normal (non string pool memory) and also in the string pool memory. So
we have two strings. The p reference will refer to the "bdf" in the
non string pool memory. Two strings are created because of the new()..

is that means there is TWO objects eligible for Garbage Collector ??
Can you help me with some sites or Articles about this ...Becouse I do't have the Book you refer to ..??


Regards


Better to light one small CANDLE than to curse the Darkness
Keith L
Greenhorn

Joined: May 23, 2004
Posts: 24
Hi Ken,

Thanks for your reply. I'm working from the book you mentioned. The example I gave was taken from question 3 on page 391. I've had a look at page 360 and I'm sure you're right. That's the answer that they were looking for.

That said, I feel somewhat uncomfortable in the use of the word "object" in this context. I don't feel that "xyz" was ever an object in the sense described in the "exam watch" paragraph on page 20. It was never instantiated.

I don't really feel that placing a string in the String Constant Pool is equivalent to creating an object with "new". There again, these are the people who wrote the exam, so if I'm asked in the exam, I most certainly *will* feel that the two things are equivalent

Keith
Ken Rubiniac
Greenhorn

Joined: May 14, 2004
Posts: 10
I feel somewhat uncomfortable in the use of the word "object" in this context. I don't feel that "xyz" was ever an object in the sense described in the "exam watch" paragraph on page 20. It was never instantiated.

I don't really feel that placing a string in the String Constant Pool is equivalent to creating an object with "new". There again, these are the people who wrote the exam, so if I'm asked in the exam, I most certainly *will* feel that the two things are equivalent.

Hi Kieth,

I see what you mean when you compare strings to putting objects on the heap,
specifically the exam watch on page 20. Strings are an exception to the rule,due to their immutablility.You just get 1 unique string ie. "abc"
in the string constant pool. It can have lots of references.


Yet every time you do a new for string "abc", you get a new string "abc" in the non-string buffer pool(heap). The 1 unique "abc" in the string constant pool still remains.
Ken Rubiniac
Greenhorn

Joined: May 14, 2004
Posts: 10
Hi Deyaeddin,

I found an interesting article at:
http://www.csc.uvic.ca/~mcheng/360/notes/simpleRTJ/GarbageCollection.html

I talks about garbage collection of the string buffer constant pool when it fills up.

Regards,

Ken
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12825
    
    5
To my mind the problem you are having is the unclear meaning of:
"how many objects are created in the code example."

13. String x = new String ("xyz") ;

It would be much clearer if one said "How many objects are created by the execution of this statement." In that case the answer is clearly 1 because the string literal String was loaded with the class and the execution creates only one object- a new String which in fact has an internal pointer to the "xyz" character sequence in the literal constant.
Here is the code from java.lang.String to prove it:



Bill
Keith L
Greenhorn

Joined: May 23, 2004
Posts: 24
Hi Bill,

This is the full text of the question:

-----
Given the following,
13. String x = new String("xyz");
14. y = "abc";
15. x = x + y;

how many String objects have been created?

A. 2
B. 3
C. 4
D. 5
----

And this is the answer:

----
C. Line 13 creates two, one referred to by x and the lost String �xyz�. Line 14 creates one (for a total of three). Line 15 creates one more (for a total of four), the concatenated String referred to by x with a value of �xyzabc�.
----

The author's rationale is that, because the statment creates a String object *and also* places the literal "abc" in the String Contant Pool, it has created two objects.

The authors are the OO experts and I'm a novice, but I'm really not convinced that two Java objects have been created. I feel that the use of the String Constant Pool is a memory saving device used by the JVM. It is more to do with the JVM's hardware implementation of the language than with the definition of the language itself.

Keith
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12825
    
    5
how many String objects have been created?

Is not a clear statement of the problem - it is sloppy and does not distinquish between compilation, loading and execution.

After years of trying and observing other people's attempts, I conclude that one should NOT use the String class in examples asking "how many objects are created."


Bill
David Ng
Greenhorn

Joined: May 11, 2004
Posts: 12
In the following code, how many objects will be created?

String x = "abc";
String y = new("abc");
String z = new("abc");

Could someone explain this case?
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
of course String literals ARE objects.
They're just not treated like other objects by the JVM in that they're pooled.

Your line creates a String object in the String pool, then creates a new String object on the heap using the String object in the pool as a template.

AFAIK the exam does not specifically NOT use Strings for questions like this.


42
Keith L
Greenhorn

Joined: May 23, 2004
Posts: 24
Maybe the confusion lies in the fact that there is no formal definition of what consititutes a Java object. A quick search came up with this which defines an object as including data and functionality. As far as I'm aware functionality is only associated with heap objects, not literals in the String Pool.

Maybe I'll start a thread in the advanced topics forum asking for a definition of object.
[ May 25, 2004: Message edited by: Keith Leng ]
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
the String literals in the pool are fully functional Strings. All methods for Strings work on them.

For the application programmer there's usually no reason to bother whether a String exists on the heap or in the pool at all, after all they're immutable so changing them doesn't work anyway.
Only when memory is tight might there be an additional penalty in pooled strings when you're not needing it for prolonged periods because garbage collection on pooled strings is slower or doesn't happen at all.
Keith L
Greenhorn

Joined: May 23, 2004
Posts: 24
the String literals in the pool are fully functional Strings. All methods for Strings work on them.


But surely they're not fully funtional by themselves. Code is required to implement functionality. They are simply the data. It's as though the answer is saying "Here we have the code and the data, that's one object; and now we'll count the data again for a total of two objects"

Page 20 of the same book contains this:

You may see the words construct, create, and instantiate used interchangeably. They all mean, �An object is built and placed on the heap."

By that definition, clearly, only one object has been "created".

Keith
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12825
    
    5
String literals are indeed fully functional String objects as far as the programmer is concerned. Try this for instance:

if( "true".equals( userinput ) ){
...
}
There you have a String literal executing the equals method.
Bill
Vishal Changrani
Greenhorn

Joined: May 20, 2004
Posts: 1
hi
well i m kinda new here..have decided to give my SCJP exam this summer..and hence m here...
i have three questions:
1. The string query that has been posted..(although i m still on the first chapter of the Osborne book)...isnt it something to do with anonymous objects.. i mean when i say String a = new String("me") new string creates an anonymous object which is assigned to a. ...just a tought..
2. What is the answer of the question posted by Davidng about the three sting x,y and z..plz someone tell me the answer..
3. And finally a classical question..is the book..by Osborne publications sufficient of do I have to do read more (plz tell me no )
k anyway guys
bye from me now
do reply
sorry if my questions sound naive...
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8883
    
    5
Arghhh!

I think we all hate this topic! It seems vague, too tied to VM specific stuff, not very useful, blah, blah, blah. Unfortunately, it's on the current exam - although it is covered very lightly - you might not get any questions on "how many String objects were created".

Unless you're trying to get 100%, focus your attentions on topics that are hard AND play a big part in the exam; inner classes, threads, GC.

So I beg you, don't shoot the messenger - months from now when we're updating the exam, we're sure to eliminate any such nonsense, but that's months and months away, so we have to live with it for now.

-Bert :roll:


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Keith L
Greenhorn

Joined: May 23, 2004
Posts: 24
Bill, how about some code that calls a method on each of the two distinct objects created by line 13?

Bert, if a question along these lines does crop up in the exam, would you suggest that we treat code similar to line 13 as though it does create two objects?

Cheers,
Keith

[ May 26, 2004: Message edited by: Keith Leng ]
[ May 26, 2004: Message edited by: Keith Leng ]
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8883
    
    5
Keith -

I'm *pretty* sure (I'd bet $5, but not my life ), that it's the 'new' that makes it 2 objects created - one on the heap, one in the pool. For sure the test does NOT get into these hair-splitting definitions of what's an object - wherever they live, heap or pool, as far as the exam is concerned they're objects.

Remember though - this is a MINOR point on the exam! (i.e. counting String objects). What's BIG on the exam is understanding when non-String objects are eligible for GC. (So counting non-String objects is important.) And a big part of that is being really clear on what's an object and what's a reference to an object - THAT"S HUGE !!!

Hope that helps.

-Bert
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How many String objects have been created?