• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String & Literal Pool Doubt

 
Lucky J Verma
Ranch Hand
Posts: 278
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have few basic doubts in String & its memory pool.
With String s=new String("abc");
Two objects with value abc ,are created.One in prgram space referenced by s.& One goes in literal pool.
1. Is this String in literal pool remains lost or unreferenced.
2. And if program furthue uses s ,value abc comes from program space?
3. Is this literal pool is created per class? each class having 1 literal pool
---------- From SCJP K&B ,Chap6,page 420------------------

Thank you
 
David Buzatto
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well...

Lets think for a while...

1 - I dont know... It should be dependent of the implementation...
2 - Yes
3 - If the pool was created by class, the problem of have zilions of equal Strings will not be solved, do you agree? If I have "abc" in Class1 and "abc" in Class2, and the pool is per class, i will have 2 "abc", expending more memory than i need. Strings are imutable, so, have two or more pools is not a good ideia since the String will never be changed...
 
Bert Bates
author
Sheriff
Posts: 8898
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i'm thinking this is more of an intermediate level question...
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now you have been transferred from beginners . . .
Look up the String API documentation, where it says String literals are shared. I think that means there is one copy and only one of every String literal; Try this:-Try different names for the command-line argument, eg "Campbell", "Lucky", "David".
Add different Strings, and repeat the test whether they are the same object (==) or identical (equals).

Go to the jakarta website and find the documentation about the bytecode engineering library, which describes how objects are put in memory in Java. See whether those suggestions sort out your problem, then report back and tell us what you found.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mistake in what I posted. Please change "sh1.getEntry()" to read "sh2.getEntry()".
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic