So I've been reading both the Sierra/Bates book and Exam Cram 2 books. There are two questions that are very similar that seem to me to give contradictory answers to one another. The first one is from the Sierra/Bates book: String x = new String("xyz"); y = "abc"; x = x + y; The answer of how many String objects that were created is 4. This is from page 391 of the Sierra/Bates book. On page 59 of the Brogden/Green book they have this code: String A,B,C; A = new String("1234"); B = A; C = A + B; Their answer to how many String objects that were created is 2. So who is right? In the line of 'String x = new String("xyz")', Sierra/Bates argue that two objects are created. In the Brogden/Green book they argue that 'A = new String("1234")' only creates one object. So who is right? Guess I'm confused now but I would think that Brogden/Green are right. Anyone know the answer? [ December 04, 2003: Message edited by: A. Fernandez ] [ December 04, 2003: Message edited by: A. Fernandez ]
Hi Vicken I suppose in the example from k&b's book 3 objects r created and in the example frfom cram book 2 objects are created. pls correct me if 'am wrong.
Joined: Nov 24, 2003
Well, Looking back at the code from page 391 of the Sierra/Bates book they don't even define what y is. What I wrote above is how they wrote the question. I guess we can presume y is a String reference....
Hi A. I think your question reduces to the following: In any expression, such as new String("abc") or s = "abc", should we count the String object created from the string literal "abc"? [ December 04, 2003: Message edited by: Marlene Miller ]
Joined: Nov 24, 2003
Yes Marlene, I think that is what the question reduces to. My thoughts right now are that maybe Kathy and Bert made a mistake on this question from their book. Personally I have found Exam Cram to have a little more clarity about some subjects that are on the exam. Possibly the Sierra/Bates book is the first edition and this is mentioned in their errata page of errors (that went to press) as a mistake. My assumption is that only three objects are created and not four. [ December 04, 2003: Message edited by: A. Fernandez ]
Joined: Mar 05, 2003
Hi A. String x = new String("xyz"); "xyz" is a string literal that represents a String object. A reference to that object is passed to the String constructor. The String constructor takes the passed reference to a String object and uses it to create a new object with "equal" contents. A reference is returned and assigned to x. y = "abc"; "abc" is a string literal that represents a String object. A reference to that object is assigned to the variable y. x = x + y; A StringBuffer object is created. The contents of two String objects are appended. A String object is created from the contents of the StringBuffer and a reference is returned and assigned to x. So I count 4 String objects altogether. [ December 05, 2003: Message edited by: Marlene Miller ]
Our intent with that example is to ask how many are created by execution of the lines of code. As Marlene pointed out, the String literal exists before the code is executed so it doesn't count. String A = new String("1234"); creates a new String object (interesting sidelight, internally, the object points to the char in the String literal.) B = A ; // reference assigned, no object created C = A + B ; // new String object created (new char with combined chars) Bill
as the String literal "1234" depends on the code String A = new String("1234"); for it's very existence (in the given the code snip), IMVHO, it most certainly 'counts'. However, I haven't read page 59 and suspect that the code, alone, is out of context.