Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does the String.intern() method return two different results? [duplicate]

I have the code like this:

String str1 = new StringBuilder("计算机").append("软件").toString();
System.out.println(str1.intern() == str1); //true

String str2 = new StringBuilder("ja").append("va").toString();
System.out.println(str2.intern() == str2); //false

String str3 = new StringBuilder("Str").append("ing").toString();
System.out.println(str3.intern() == str3); //true

I can understand why str1.intern() == str1 and str3.intern() == str3 are true, but I don't understand str2.intern() == str2. Why this is false?

My java version is: 1.8.0_73

like image 549
Timi Avatar asked Mar 17 '16 14:03

Timi


People also ask

What is the purpose of intern () method in the string class?

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.

What is String intern () When and why should it be used?

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.

What is the use of the intern () method 1 point?

The intern() method creates an exact copy of a String that is present in the heap memory and stores it in the String constant pool. Takeaway - intern() method is used to store the strings that are in the heap in the string constant pool if they are not already present.

What is the use of intern () method show with small example?

Java String intern() method explained with examples This method ensures that all the same strings share the same memory. For example, creating a string “hello” 10 times using intern() method would ensure that there will be only one instance of “Hello” in the memory and all the 10 references point to the same instance.


2 Answers

String.intern() returns a String in the string literal pool. However if the string already exists in the pool, it will return the existing String.

If you pick a new String, it should return the String you created, but if you use a String which happens to exist in the pool already you will get the existing String.

It is reasonable to assume that in this case "java" already exists in the pool so when you call intern() it returns a different object so == is false.

note: string.intern().equals(string) should always be true.

like image 123
Peter Lawrey Avatar answered Nov 15 '22 18:11

Peter Lawrey


The constant String "java" already exists in the Java constant pool, but you can verify that by changing

String str2 = new StringBuilder("ja").append("va").toString();
System.out.println(str2.intern()==str2);

to

String str2 = new StringBuilder("ja").append("va").toString();
System.out.println(str2.intern() == "java");

which will get the same constant and output

true

Alternatively, you could add "计算机软件" and "String" to the constant pool like

String a = "计算机软件";
String b = "String";
String str1 = new StringBuilder("计算机").append("软件").toString();
System.out.println(str1.intern() == str1);

String str3 = new StringBuilder("Str").append("ing").toString();
System.out.println(str3.intern() == str3);

Then you would get (consistent with your observations)

false
false
like image 32
Elliott Frisch Avatar answered Nov 15 '22 17:11

Elliott Frisch