• 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

How many object created ?

 
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello all,
Could anyone tell me how many objects created in the following code ?
The code is copied from book: Sun Certified Programmer & Developer for Java 2 Study Guide (Exam 310-035 & 310-027)

The answer are 4 objects but I only count 3, is the answer correct or not ?
Thanks in advance!
Jack
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. "abc"
2. Object referenced by x
3. "cde"
4. Object referenced by z
 
Ranch Hand
Posts: 218
VI Editor Ruby Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would have thought the objects that were created:
1. String x
2. String "cde" which referred by y
3. StringBuffer for x + y
4. String z
 
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Optimization of String Concatenation
An implementation may choose to perform conversion and concatenation in one
step to avoid creating and then discarding an intermediate String object. To
increase the performance of repeated string concatenation, a Java compiler may
use the StringBuffer class or a similar technique to reduce the number of intermediate
String objects that are created by evaluation of an expression.


15.18.1.2 Optimization of String Concatenation
From this quote, creation of a StringBuffer is not ever guaranteed. I don't see an intermediate String object that can be discarded either. Assuming that the code beyond the discussed fragment didn't create String literals "abc" and "cde", there're 4 objects.
[ October 07, 2003: Message edited by: Vad Fogel ]
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I count only three
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic