GeeCON Prague 2014*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes So who's right about how many String objects are created (Sierra or  Brogden)? 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 "So who Watch "So who New topic
Author

So who's right about how many String objects are created (Sierra or Brogden)?

A. Fernandez
Greenhorn

Joined: Nov 24, 2003
Posts: 9
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 ]

Lovin' Life.....
Vicken Karaoghlanian
Ranch Hand

Joined: Jul 21, 2003
Posts: 522
Hi,

I believe this will create 2 objects.
However i am not saying that K&B are wrong .The code you posted is not clear because the 'y' variable is not defined.


- Do not try and bend the spoon. That's impossible. Instead, only try to realize the truth. - What truth? - That there is no spoon!!!
Mona Gadkari
Ranch Hand

Joined: Jul 28, 2003
Posts: 64
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.


Mona(Varijasmom)
A. Fernandez
Greenhorn

Joined: Nov 24, 2003
Posts: 9
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....
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
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 ]
A. Fernandez
Greenhorn

Joined: Nov 24, 2003
Posts: 9
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 ]
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
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 ]
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12791
    
    5
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
Ray Stojonic
Ranch Hand

Joined: Aug 08, 2003
Posts: 326
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.
 
GeeCON Prague 2014
 
subject: So who's right about how many String objects are created (Sierra or Brogden)?