Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes It's about the String pool: I would like to know which String goes into the pool Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "It Watch "It New topic
Author

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

Cyrus Hackford
Greenhorn

Joined: Mar 15, 2010
Posts: 2
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?


System.out.println("Hello, Dolly!");
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13884
    
  10

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.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Cyrus Hackford
Greenhorn

Joined: Mar 15, 2010
Posts: 2
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

Joined: Sep 14, 2009
Posts: 1623
    
  10

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.


When you do things right, people won't be sure you've done anything at all.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: It's about the String pool: I would like to know which String goes into the pool
 
Similar Threads
Capturing UTF-8 from an exec statement
question about socket encoding problem
85 line program...wondering if it's possible to do in Applets
Multiple sockets cause Threads Hangin
convert uft-8 into ascii format