• 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

Sybex question!

 
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is the code fragment:

Question is: How many objects are created by te following code?

A) None.
B) 1.
C) 2.
D) 3.

The correct answer is D.
My answer was C. Can anyone explain this please.
Thanks, Dave.
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I also would have answered C, that 2 objects are created. Was there an explanation given with the answer?
 
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
The argument might be that the "abc" caused creation of an interned String - an argument that I disagree with.
It just goes to show that Strings are not good for use in "how many objects" questions.
Bill
 
Dave Johnson
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The explanation given is as follows:

Objects created at runtime are not placed in the constant pool, while the literal String "abc" is placed there. Therefore, three objects are created. One, the String "abc", is created at class load time; the other two are StringBuffer objects created at the time the code is executed.
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No objects are created by the compiler. No objects live in the constant pool. All objects are created at runtime and inhabit the heap. String objects that are interned happen to be referenced from the String pool. The String pool is not the constant pool of a class.
The String object pointed to by a string literal is created the first time such literal is used, and interned. Subsequent string literals with the same content are pointing to such String object. It is like magic !
[ September 15, 2003: Message edited by: Jose Botella ]
 
Dave Johnson
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So now that I have told you their explanation to the answer. Is the official answer still right?
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes of course D is right.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic