This week's book giveaway is in the General Computing forum.
We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line!
See this thread for details.
The moose likes Java in General and the fly likes question regarding String.intern() Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "question regarding String.intern() " Watch "question regarding String.intern() " New topic
Author

question regarding String.intern()

amitabh mehra
Ranch Hand

Joined: Dec 05, 2006
Posts: 98
From java specification (3.10.5) String literals :

package testPackage;
class Test {
public static void main(String[] args) {
String hello = "Hello", lo = "lo";
System.out.print((hello == "Hello") + " ");
System.out.print((Other.hello == hello) + " ");
System.out.print((other.Other.hello == hello) + " ");
System.out.print((hello == ("Hel"+"lo")) + " ");
System.out.print((hello == ("Hel"+lo)) + " ");
System.out.println(hello == ("Hel"+lo).intern());
}
}
class Other { static String hello = "Hello"; }

and the compilation unit:

package other;
public class Other { static String hello = "Hello"; }

produces the output:

true true true true false true

This example illustrates six points:

* Literal strings within the same class (§8) in the same package (§7) represent references to the same String object (§4.3.1).
* Literal strings within different classes in the same package represent references to the same String object.
* Literal strings within different classes in different packages likewise represent references to the same String object.
* Strings computed by constant expressions (§15.28) are computed at compile time and then treated as if they were literals.
* Strings computed at run time are newly created and therefore distinct.
* The result of explicitly interning a computed string is the same string as any pre-existing literal string with the same contents.



Also javadoc says:
"Returns a canonical representation for the string object.

A pool of strings, initially empty, is maintained privately by the class String.
When the intern method is invoked, if the pool already contains a string equal to this String object as determined by the equals(Object) method, then the string from the pool is returned. Otherwise, this String object is added to the pool and a reference to this String object is returned.

It follows that for any two strings s and t, s.intern() == t.intern() is true if and only if s.equals(t) is true.

All literal strings and string-valued constant expressions are interned."


Is the pool that is being talked about here, different from the string literal pool?
If no, then the 5th println in above mentioned example should have been true. Please explain.

Thanks
Jesper de Jong
Java Cowboy
Bartender

Joined: Aug 16, 2005
Posts: 12950
    
    3

You're talking about this line (which produces the 5th answer "false"):

Note the rules:
* Strings computed by constant expressions (§15.28) are computed at compile time and then treated as if they were literals.
* Strings computed at run time are newly created and therefore distinct.

The expression "Hel"+lo is computed at runtime, and therefore creates a new String object that is distinct from the String object that hello refers to.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
amitabh mehra
Ranch Hand

Joined: Dec 05, 2006
Posts: 98
I am a bit confused about the literal pool.

Point me if I am wrong:
To cut down the number of String objects created in the JVM, the String class keeps a pool of strings. So each time a new string is generated, this pool is checked first and if it exists in the pool, a reference to it is returned. else, new string object is created and also added to pool.
Now, when I say "Hel" + lo, which finally results in "Hello", and a new object is returned (since its at runtime), does it mean that pool is now having 2 objects with same content :"Hello"?
amitabh mehra
Ranch Hand

Joined: Dec 05, 2006
Posts: 98
Ranchers, can someone clear the doubt?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19232

The string pool has at most one version of each string constant. That's why String.intern() will return the exact same object references:


SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
amitabh mehra
Ranch Hand

Joined: Dec 05, 2006
Posts: 98
Rob Prime wrote:The string pool has at most one version of each string constant.


Rob, Thats exactly my doubt. When I said String s1 = "hello", an entry for hello has been added into the literal pool. Now when I created String s2 = new String("hello"), what will happen?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19232

It will create a new object on the heap, but not in the string pool.

From the API of String.intern():
Returns a canonical representation for the string object.

A pool of strings, initially empty, is maintained privately by the class String.

When the intern method is invoked, if the pool already contains a string equal to this String object as determined by the equals(Object) method, then the string from the pool is returned. Otherwise, this String object is added to the pool and a reference to this String object is returned.

It follows that for any two strings s and t, s.intern() == t.intern() is true if and only if s.equals(t) is true.

All literal strings and string-valued constant expressions are interned. String literals are defined in §3.10.5 of the Java Language Specification.

Using string literals will add those to the string pool automatically, and string literals are also retrieved from the string pool.

So with "String s2 = new String("hello")", it will fetch "hello" from the string pool, and use its contents to create a new object.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: question regarding String.intern()
 
Similar Threads
String Objects
string question .explanation pls
Help please
inner class doubt
String related query