• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

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

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 522
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
A. Fernandez
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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....
 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
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
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
 
Ranch Hand
Posts: 326
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
He does not suffer fools gladly. But this tiny ad does:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic