Possible Duplicate:
intern() behaving differently in Java 6 and Java 7
While doing example for this question
I noticed a strange behaviour of intern()
method when I call intern()
method on String
thereafter I can use ==
operator for the Original String.
JavaDoc of intern()
method:
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
andt
,s.intern() == t.intern()
is true if and only ifs.equals(t)
is true.
Above Javadoc does not say that the orginal string gets changed. So why this program prints okay
when test
is the input.
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner user_input = new Scanner(System.in);
String username;
System.out.print("username: ");
username = user_input.next();
// Even if I do not assign returned string for comparison still it compares
// okay else it does not compare
username.intern();
if (username == "test") {
System.out.println("okay");
}
else {
System.out.println("not okay");
}
}
}
intern() The method intern() creates an exact copy of a String object in the heap memory and stores it in the String constant pool. Note that, if another String with the same contents exists in the String constant pool, then a new object won't be created and the new reference will point to the other String.
String Interning is a method of storing only one copy of each distinct String Value, which must be immutable. By applying String. intern() on a couple of strings will ensure that all strings having the same contents share the same memory.
The distinct values are stored in a string intern pool. The single copy of each string is called its intern and is typically looked up by a method of the string class, for example String. intern() in Java. All compile-time constant strings in Java are automatically interned using this method.
Intern strings are stored in a string pool in the JVM memory. JVM Memory has the following regions: Heap region (i.e. Young & Old generation) Metaspace.
String.intern() returns the intern()ed String. This is because a method cannot change a reference passed to it.
So why this program prints okay when test is the input.
It prints okay because the intern()ed string is the first time this String is seen so it becomes the one String literal for "test". It is not the String which is changed, but the object which is used for "test" which is changed.
Try instead.
String te = "te", st = "st";
// "test".length();
String username = te + st;
username.intern();
System.out.println("String object the same is: "+ (username == "test"));
In this case, the output in Java 7 update 7 is
String object the same is: test
but run this on Java 6 update 32 or uncomment the line so "test" is seen first and you get.
String object the same is: false
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With