• 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

How many String objects have been created?

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

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
 
Keith L
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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

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
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:
 
Keith L
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
reply
    Bookmark Topic Watch Topic
  • New Topic