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

BJO - String literal pool contains object ???

 
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Hi Jacquie,

This discussion has happened many times here that, Whether String literal pool contains object or reference???

String s = "hello";

According to me, one object get created ("Hello") and that is on String literal pool.

String s1 = new String("Hi");

Now, 2 objects get created, one is on heap that has internally reference of another object "Hi" and this is on String literal pool.

What you say??
This is really a confusing topic...

Thanks a lot.
 
Ranch Hand
Posts: 657
Spring VI Editor Clojure
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

According to me, one object get created ("Hello") and that is on String literal pool.


According to both the language specification and the VM spec, the String object is created on the heap, and a reference is stored in the runtime constant pool.
 
ankur rathi
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator


According to both the language specification and the VM spec, the String object is created on the heap, and a reference is stored in the runtime constant pool.



You mean to say, string literal pool doesn't contain objects. So in that case:

String s = new String("Hello");

Here only one object is getting created instead of two???

I think, something is wrong, in my opinion 2 objects are getting created..

Please comment.

Thanks.
 
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Rathi, according to the JLS, a String literal is a reference to an object. So the pool of string literals is a pool of references, not a pool of objects. In the case of

String s = new String("Hello");

you are correct, two objects get created.

First, an object on the heap is created, and is referenced by the string literal "Hello" which is a reference stored in the string constant pool.

Second, another object is created on the heap by the new keyword and the String constructor, and a reference to that second object is stored in the variable s.
 
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:
  • Report post to moderator
That's the last word. If you want to talk about this more, take it to SCJP, please.
 
Don't get me started about those stupid light bulbs.
    Bookmark Topic Watch Topic
  • New Topic