• Post Reply Bookmark Topic Watch Topic
  • New Topic

String intern  RSS feed

 
J Mohandos
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am unable to understand the use of String intern. Why not use Code 1 as given below instead of using intern as in Code 2? Both perform the same functionality.

Code 1:



Code 2:


 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String.intern() puts the string that you pass it in the string pool if it is not already there. You rarely need to use this method; it is only for very special optimization cases. I can't remember ever needing to call this method.

In the example you posted, there is indeed no difference at all, since you are working with literal strings, which are already in the string pool.

A special case where you might want to use this is if you are for example reading strings from a file, and you expect the same string to appear many times. By interning the string, you would have only one copy of the string in memory, instead of many String objects with the same content. But there's also a danger here, strings in the string pool will not normally be garbage collected (because the string pool has a reference to those strings), so you might create a memory leak in your application by doing this.
 
J Mohandos
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By interning the string, you would have only one copy of the string in memory, instead of many String objects with the same content

Is this not how it works internally? When it is already happening that way internally, I don't understand what is the need for me to use it.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
J Mohandos wrote:Is this not how it works internally? When it is already happening that way internally, I don't understand what is the need for me to use it.

Not all String objects are automatically interned.

If you create multiple String objects explicitly, for example like this:

then you will have three String objects in memory, with the same content - but they will be three separate objects.

String literals are automatically interned, which means that only one String object is created, which is shared:

If you explicitly create String objects with the new operator, then those strings are not automatically interned.
 
J Mohandos
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok. I understood partially. Lets take a simple example. I don't want to go too deep into the topic but at the same time I don't want to leave a doubt in my mind.

Suppose I am reading 10 values from a file. "hello1" occurs 4 times, "hello2" occurs 3 times, "hello3" occurs 3 times. Now in this example should I assume the file should contain "new String("hello1")" or "hello1"?
 
Dave Tolls
Ranch Foreman
Posts: 3068
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
J Mohandos wrote:Ok. I understood partially. Lets take a simple example. I don't want to go too deep into the topic but at the same time I don't want to leave a doubt in my mind.

Suppose I am reading 10 values from a file. "hello1" occurs 4 times, "hello2" occurs 3 times, "hello3" occurs 3 times. Now in this example should I assume the file should contain "new String("hello1")" or "hello1"?


Look at it this way, unless there is a call in the code to the intern() method, it's the compiler that "interns" Strings.
So, in your example, does the compiler know of the contents of your file?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!