• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

It's about the String pool: I would like to know which String goes into the pool

 
Cyrus Hackford
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have already searched this forum; with "string pool not literal", "string pool", etc. But I failed to grab appropriate answers.
My question is this: Do Strings that are gained from file or socket reading go into the pool?

Let's see following code: (code from <Head First Java>, Appendix B)

I know that 10 strings will be created and they all go into the pool.

But how about this:

This String object 'inline' is a programmatically generated String.
Do the values of 'inline' fall into the String pool? Do they remain in memory until JVM stops? Or, does GC just slaughter all that 'inline's?
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15216
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch.
Cyrus Hackford wrote:I know that 10 strings will be created and they all go into the pool.

Why do you think that? I don't think it's true. String objects that represent string literals, such as the "0" in your example, will go into the string pool. But other String objects, such as the ones you create in the loop, will not.
Cyrus Hackford wrote:This String object 'inline' is a programmatically generated String.
Do the values of 'inline' fall into the String pool? Do they remain in memory until JVM stops? Or, does GC just slaughter all that 'inline's?

No, they will not go into the string pool and remain in memory until the JVM stops. Not all strings that are "programmatically generated" (whatever that means exactly) will go into the string pool.
 
Cyrus Hackford
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper Young wrote:Welcome to JavaRanch.

Thank you! :D

Jesper Young wrote:Why do you think that? I don't think it's true. String objects that represent string literals, such as the "0" in your example, will go into the string pool. But other String objects, such as the ones you create in the loop, will not.

But, in <HFJ>, writers said that they will remain on memory.
HFJ wrote:9 Strings that are created by the loop will remain; they do not disappear

Is the book wrong? Oops...

And,
Jesper Young wrote:Not all strings that are "programmatically generated" (whatever that means exactly) will go into the string pool.

This is what I was looking for. XD
"Programmatically generated" means that, it was not specified on source code; it appears during runtime through file reading or something else. At least I used the expression in that meaning.


Again, thank you very much!
Have a nice day, Jesper! (It is night here~ :D)
 
Janeice DelVecchio
Saloon Keeper
Posts: 1808
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check this out:

http://www.javaranch.com/journal/200409/Journal200409.jsp#a1

... my brief understanding is this (and correct me if I'm wrong)...

String literals (declared in quotes, or as new String("some string")) go into the pool and stay until JVM is shut down.
Other Strings go into the pool and are GC when all references to the String is lost.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic