File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes String & Literal Pool Doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "String & Literal Pool Doubt" Watch "String & Literal Pool Doubt" New topic
Author

String & Literal Pool Doubt

Lucky J Verma
Ranch Hand

Joined: Apr 11, 2007
Posts: 278
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

Joined: Jul 16, 2006
Posts: 9
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...


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." (Fowler)
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8815
    
    5
i'm thinking this is more of an intermediate level question...


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38472
    
  23
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

Joined: Oct 13, 2005
Posts: 38472
    
  23
Mistake in what I posted. Please change "sh1.getEntry()" to read "sh2.getEntry()".
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String & Literal Pool Doubt