• 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

String Object Creation

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a Question regarding the String Class

String s = new String("abc")

As per the defintion of invoking a overloaded constructor it create an object in the non pool memory and assigns it to reference variable s.In addition creates one more object abc and keep it in pool. Please correct me if something is incorrect or missed.

Can anybody please explain this in detail ??
 
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
any time the compiler sees a string literal, it creates one in the string pool (although it won't create a duplicate if you have "abc" twice)

so, in your example, there are two string-like objects created, one in the pool, and one in 'non-pool' memory. So your understanding is correct.

BOTH objects are immutable.

Note that you can still point your string reference to a NEW string, but the old one stays there unchanged...
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually, no objects are created in the string literal pool. The pool contains only String references.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Joanne Neal:
Actually, no objects are created in the string literal pool. The pool contains only String references.



Indeed, this is true, although I recently spent some time up to my elbows in the Sun's JVM source, and it turns out that interned Strings objects are moved to a special "permanent generation" subheap, outside of the normal Java heap -- so it's not quite as cut and dried as the traditional party line would have it!
 
Debi Pany
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
so Joanne what you meant is only one reference and one object is created.
please explain this in detail.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Debi Pany:
so Joanne what you meant is only one reference and one object is created.
please explain this in detail.



No. Two objects are created, but they are both created on the heap (but see EFH's post). The String literal pool only contains references to String objects, not the objects themselves.
 
Ranch Hand
Posts: 257
Hibernate Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

To maintain String objects, java uses flyweight design pattern.
 
reply
    Bookmark Topic Watch Topic
  • New Topic