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
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 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.
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.
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.
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
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