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 String constructors and counting objects 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 "String constructors and counting objects" Watch "String constructors and counting objects" New topic
Author

String constructors and counting objects

Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
I want to revisit the familiar question...
How many objects are created by the following expression?
new String(�abc�)
How many objects are created by the following expression?
new String(new char[0])
What do you think the answers are?
Rory French
Ranch Hand

Joined: Apr 03, 2003
Posts: 97
Hi Marlene
new String("abc")
I think it creates 2 objects. The first object is the literal "abc" passed to the String constructor, and the second is the String object created by the new keyword
new String(new char[0])
Again, I think 2 objects are created. One zero length char array object and one String object.
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
Hi Rory,
Thank you for volunteering your answer. Now let�s discuss what is on my mind.
The Java Programming Language (a trustworthy book, would you agree?) says the constructor String(char[] chars) makes a copy of the array, so you can change the array contents after you have created a String from it without affecting the contents of String.
To me that means new String(new char[0]) creates 3 objects.
What do you think?
[ May 05, 2003: Message edited by: Marlene Miller ]
Roger Chung-Wee
Ranch Hand

Joined: Sep 29, 2002
Posts: 1683
There may be three objects, but as two look unreferenced, then they would immediately be available for gc. (I presume that this is about gc.)
So, for exam purposes, there is one String object on the heap which would be available for gc once it is no longer reachable from a live thread.


SCJP 1.4, SCWCD 1.3, SCBCD 1.3
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
I would rather not discuss gc. I am just counting the number of objects created by the expression.
Rory French
Ranch Hand

Joined: Apr 03, 2003
Posts: 97
Marlene.
I do agree that The Java Programming Language is a trustworthy book, since its straight from the horse's mouth. Although I am ashamed to say I havn't read a bit of it.
I've been looking at the Java documentation on the constructors for the String object, and it says the following

String(char[] value)
Allocates a new String so that it represents the sequence of characters currently contained in the character array argument.
String(String original)
Initializes a newly created String object so that it represents the same sequence of characters as the argument; in other words, the newly created string is a copy of the argument string.

Note that for a String argument, "the newly created string is a copy of the argument string",
which is similar to the statement you have raised about passing a char[] argument.
The documentation also says that for a char[] argument, the String represents the sequence of characters of the char[] argument.
So, I still think that in both cases, only two objects are created. So when the book says "the constructor String(char[] chars) makes a copy of the array", I think it means the String contains a (String)representation of the array (an this is the first object), which cannot be changed by changing the array (the second object).
Of course I am speculating and I offer this information on the proviso that I can change my mind if proven wrong(so I can save face you understand )
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
Rory, your comments made me curious about just what the String representation is. This is what I found.

So a String object has a reference to an array of char.
On one hand, we wouldn�t be expected to know or account for objects that are created internally.
On the other hand, I think I remember Kathy saying The Java Programming Language is fair game for the exam.
Rory French
Ranch Hand

Joined: Apr 03, 2003
Posts: 97
So three objects are in fact created. Interesting.
Thanks for checking it out Marlene. Okay so maybe after I've eaten my humble pie I might get myself a copy of The Java Programming Language.
[ May 05, 2003: Message edited by: Rory French ]
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
Thank you Rory for working through this idea with me.
Kathy Sierra
Cowgirl and Author
Ranch Hand

Joined: Oct 10, 2002
Posts: 1572
Originally posted by Marlene Miller:

On the other hand, I think I remember Kathy saying The Java Programming Language is fair game for the exam.

Howdy
This book is one of our two references for the exam (the other being the JLS). But not everything in the book is covered by the exam objectives. The issue you're bringing up is *definitely* not covered under the exam, although you wouldn't necessarily know that from reading the exam objectives. For the most part, you are not expected to have any knowledge of a class implementation, beyond what's in the API. So in class String, for example, the API docs refer to a String as something that contains a character 'sequence'. It doesn't specifically describe its internal representation. So *you* aren't supposed to really know anything beyond the fact that Strings are immutable, and beyond what is known about the String pool (and a lot of things people speculate on, or even *know* are not necessarily in the spec, and are considered implementation details for a JVM. For example, you won't be asked *where* in memory the String pool "lives").
I'll just come right out and say that you will not have any questions about this. In fact, the notion of knowing how many objects are created by making a new String is much less important for the exam than knowing the implication of a String being immutable (like, all those questions on the mock exams that try to trick you into thinking you're changing a String when you call, say, myString.toUpperCase()).
On the real exam, we tried to get rid of garbage collection questions that involved Strings, although we weren't 100% successful (which is why there are a couple questions about it in our book). So you do still need to be aware that literals are put in the String pool, and that new String() creates a new object, even if that same character sequence is in the pool already. Beyond that, you don't need to know if all intermediate concatenations will be in the String pool. For example, String s = "foo";
String t = "bar";
String x = "baz:
String y = s + t + x;
You can only say with certainty that four Strings have been made (assuming you don't see any code that has previously put one of these Strings into the String pool)... the "foo", "bar", "baz", and the final result of "foobarbaz". You do not know, and won't be asked, if there is also a String created from the s+t concatenation, because this is not specified in the Java spec.
So, anything in The Java Programming Language *is* fair game, *if* it's covered under the objectives (and a lot of what is in that book, and the spec as well, is *not* in the objectives).
Sometimes, though, the SCJP objectives are so vague that it's hard to know for sure, which is why I try to always answer questions like this.
If it isn't in our book, it is very unlikely that it's on the exam. There are only one or two things that you *might* (depending on which version of the exam you get) see on the exam, that we did not go into in the book, but that's because the return-on-time-and-effort-investment was not justified by that topic. Like, you could quite easily pass the exam without really knowing all the conditions under which you might get NaN, for example.
On the other hand, most of the things we spent a lot of time on, are likely to be important in the real exam. Every objective in the exam has a 'weighting' (which is not published, although one could infer from reading certain materials ) and that weighting determines exactly how many questions you might get on the exam for that objective. For a great number of objectives, there is only a single question (two at the most) on the real exam, so for those, if the effort required to get that one question correct is really severe and potentially confusing, we don't say much about it in the book. It's not like you don't have enough to worry about for this exam.
That said, we are big fans of the Gosling/Arnold book (especially when contrasted with the JLS, for readability).
cheers,
Kathy
[not completely unbiased... Ken Arnold just endorsed our upcoming O'Reilly book , so we're big fans of his right now.]
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
Thank you very much Kathy for your detailed explanation and valuable insights.
I did something wrong. I should not have put words into your mouth. I should have searched the forum for exactly what you did say. It�s not fair to you. So to correct this mistake, here is what I remember Kathy saying previously (second paragraph, first sentence) the JPL and the exam
[ May 05, 2003: Message edited by: Marlene Miller ]
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
The Java Programming Language (9.8) discusses how the compiler evaluates the expression �<� + quote + �>�. They say the compiler uses a StringBuffer object to build strings from expressions, creating the final String only when necessary. Using StringBuffer, the previous string expression would be represented as
new StringBuffer().append(�<�).append(quote).append(�>�).toString()
So someone who studied the JPL would think string concatenation creates not only the final String object, but also an intermediate StringBuffer object.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: String constructors and counting objects
 
Similar Threads
String objects
How many String objects have been created?
How many objects ?
String
counting String objects