• 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

Does String s = new String("text') create one or two objects ? Authors differ

 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, wondering if anyone could clear up this up ..

In Mala Gupta's book (page 177) she says that String s = new String("text"); creates a String object
but that object is not placed in the String constant pool.

However, in Sierra & Bates (page 265) the authors state that the above will create a new String
object in normal (non pool) memory, and will also place the literal "Morning" in the pool - they say
that "two objects are created".

Which author is correct ?

Also, does anyone know how to call the overridden .toString() method (the version in Object)
on a String ?

As always, any help would be greatly appreciated.

John
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Both are correct.

When the class is loaded, all String literals and other String compile‑time constants are put into the String pool if they are not there already. So at class loading the literal "text", which constitutes a String object in its own right, is placed in the String pool. Every occasion when that line is executed, a second String object corresponding to the new String is created in an ordinary part of the heap. In one book they are writing about the whole lifetime of the JVM and in the other about what happens when that line is executed, assuming that "text" has already been created.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

John Mulholland wrote: . . .
Also, does anyone know how to call the overridden .toString() method (the version in Object)
on a String ?
. . .

You mean unoverridden? Yes, I do know. It is impossible.
 
John Mulholland
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Ritchie - very helpful as always.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome

I presume you know why it is impossible to call the unoverridden toString method? You can roll your own by investigating Object#toStringYou can fill in 95% of the missing code just by copying off the toString link. Who knows? There might already be such a method in the Objects class or elsewhere. Nothing found in Objects class.
reply
    Bookmark Topic Watch Topic
  • New Topic