This week's book giveaway is in the Java in General forum.
We're giving away four copies of Event Streams in Action and have Alexander Dean & Valentin Crettaz on-line!
See this thread for details.
Win a copy of Event Streams in Action this week in the Java in General forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Sybex question!

 
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • Quote
  • Report post to moderator
Yes of course D is right.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!