the method StringBuilder.append() , has changed the result of String.intern() [duplicate] - java

This question already has answers here:
The return of String.intern() explained
(3 answers)
Java String intern function result problem [duplicate]
(1 answer)
String builder not using string literal from string pool. Why?
(4 answers)
Closed 1 year ago.
String s1 = new StringBuilder("dsada").append("dfgdfgd").toString();
String s3 = new StringBuilder("aaaa").toString();
System.out.println(s1.intern() == s1); //true
System.out.println(s3.intern() == s3); //false
the s1 and s3 finally invoke StringBuilder.toString,
the method toString() contrust a new String(),and then invoke the String.intern(),the result is different,it confused me a lot.
#Override
public String toString() {
// Create a copy, don't share the array
return new String(value, 0, count);
}

From https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/String.html#intern():
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.
"aaaa" is a string literal in your code, so it's already in the interned pool. When you call intern() on a string with value "aaaa", it returns the existing string object that it retrieved from the pool. That's not the same object as s3.
"dsadadfgdfgd" is not a string literal in your code, so when you call intern(), it adds that string object to the pool, and then returns it, the same object.

Related

Java String intern function result problem [duplicate]

This question already has answers here:
The return of String.intern() explained
(3 answers)
Closed 3 years ago.
I'm learning Java now, and i read this question:What is Java String interning? - Stack Overflow
but i read other articles which provide some examples i don't understand:
public static void main(String[] args) {
String str2 = new String("str") + new String("01");
str2.intern();
String str1 = "str01";
System.out.println(str2 == str1); //true
String s4 = new String("1");
s4.intern();
String s5 = "1";
System.out.println(s4 == s5); //false
String s = new StringBuilder("aa").append("bb").toString();
String s2 = new StringBuilder("cc").toString();
System.out.println(s.intern() == s); //true
System.out.println(s2.intern() == s2); //false
}
the result in Java 11( it should be same in Java 8) is :
true
false
true
false
i don't know why results are different, i suppose it to be all true.
can anyone explains it?
Let's look at what intern actually does (emphasis mine):
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.
When this code is run:
str2.intern();
The string pool only contains "str" and "01". str2 is "str01", which is not in the string pool, so the object that str2 refers too is added to the string pool. Therefore, when the next line is reached, since "str01" is already in the pool, str1 will refer to the same object as str2.
Note that the reason why str2 == str1 is not because intern somehow changes which object str2 refers to. It doesn't do anything to str2. Strings are immutable after all.
Now we can understand the first false. In the line:
String s4 = new String("1");
Because of the string literal "1", "1" is added to the string pool. Note that the object that is added to the string pool is not the same as the object to which s4 refers. Now you call s4.intern(), which does nothing to s4, and returns the object that is in the string pool. But you are ignoring the return value. This is why s4 != s5, and s4.intern() == s5.
The reason for s2.intern() != s2 is much simpler - s2 refers to a different object than "cc" in the string pool! s2.intern() is supposed to return the object in the string pool, so of course it is not the same object!

String class object [duplicate]

This question already has answers here:
Difference between heap memory and string pool [duplicate]
(2 answers)
Closed 3 years ago.
There is a rule in Java, that to create any object of class we have to use 'new' keyword, but when we use String class,we can create object as
String s = "hello";
so we haven't used new as an operator still new object has been created in String constant pool in heap!
Can anyone explain how we created an object without using new keyword!
Comparision of string initialization performance for String Literal and String object. :
String Literal
String str = “Hello”;
This is string literal. When you declare string like this, you are actually calling intern() method on String. This method references internal pool of string objects. If there already exists a string value “Hello”, then str will reference of that string and no new String object will be created.
String Object
String str = new String(“Hello”);
This is string object. In this method JVM is forced to create a new string reference, even if “Hello” is in the reference pool.

Comparing two equal Strings after intern() method [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
String comparison confused
(3 answers)
Closed 6 years ago.
Javadoc says that if in the string pool there is an equal String that the intern() method will return the String.
public class Demo {
public static void main(String[] args) {
String str1 = "Apple";
String str2 = new String("Apple");
System.out.println(str1.intern() == str2); //false
System.out.println(str1 == str2.intern()); //true
}
}
I expected to get true in both cases.
System.out.println(str1.intern() == str2); //false
In the above case you are comparing reference of interned String "Apple" with that of reference of another String which is on the heap (but with the same value). So, the result is "false".
System.out.println(str1 == str2.intern()); //true
In the above case, you are comparing a reference of String constants pool to a reference got by trying to add "Apple" to the String constants pool. SInce, "Apple" is already in the first line, this interning will return the object pointed to by str1. Hence you get true.
PS : This behavior is described in the javadoc
Doing this str1.intern() is bringing nothing to your try...
Since str1 is a literal string (it will be inmediatly placed in the string pool), and you are comparing it against the str2 which is allocated on the Heap...
therefore
System.out.println(str1.intern() == str2);
will print false, you are comparing the reference of a string in the stirng pool vs a string on the heap...
String.intern() method is used to create an exact copy of heap String object in String constant pool. The String objects in the String constant pool are automatically interned but String objects in heap are not
System.out.println(firstString.intern() == secondString);//false because it will fetch exact copy of the string compare in string
System.out.println(firstString == secondString.intern());//True because it will compare firstString with fetch the second String it will check heap or not

java "== " unusual behaviour [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
== and .equals() not working in java [duplicate]
(1 answer)
Closed 6 years ago.
import java.lang.String;
public class Test {
public static void main(String[] args) {
String a1="ka";
String a2="ka";
System.out.println("a1==a2? "+(a1==a2));
String a3="k";
String a4=new String("k");
System.out.println("a3==a4? "+(a3==a4))
System.out.println("a3==a4? "+(a3==a4.intern()));
String a5="k";
String a6=a4+"a";
System.out.println("a1==a6? "+(a1==a6));
}
}
Output that i got:
a1==a2? true
a3==a4? false
a3==a4? true
a1==a6? false
a1===a2 is true as line 5 will not create new String literal in string pool area.Only reference to previously created string is returned.
a3==a4? false as a4 will have refernce to the String object instead of the string in the string in string pool area. My question is if a3 is referencing the string constant instead of the String object, how is it able to use the methods of the String class?
a4.intern() will return the reference to the string in the string pool which happens to be same as a3
a6=a4+"a" will create a new string "ka". But this actually make use of StringBuilder class and its append method . It is then converted to string using toString(). Does this process store the newly created string "ka" in the string pool area? Since the string is already in the pool the code at line 12 should return the reference to it. So the a1==a6 should be true.rt?
I am new to java. Please guide me where i am doing the mistake?
You are comparing the Strings wrongly (because you are in fact comparing references)
String Class in java is defined in java.lang package and it is exactly that, a class and not a primitive like int or boolean.
String is immutable and final in Java and in this case JVM uses String Pool to store all the String objects.
What are different ways to create String Object?
We can create String object using new operator like any normal java class or we can use double quotes (literal assignment) to create a String object.
To your Question:
When we create a String using double quotes, JVM looks in the String pool to find if any other String is stored with same value. If found, it just returns the reference to that String object else it creates a new String object with given value and stores it in the String pool.
When we use new operator, JVM creates the String object but don’t store it into the String Pool. We can use intern() method to store the String object into String pool or return the reference if there is already a String with equal value present in the pool.
So when you do
String s1 = "abc";
String s2 = "abc";
those are checked in the StringPool and since s1 already exist there, s2 will take the same reference, hence, s1 ==s2 is true.
but when you do:
String s3 = new String("abc");
String s4 = new String("abc");
you are using the new operator, therefore the JVM is not checking if there is an string already in the heap, it will just allocate a new space for s4, so is s3==s4 ??? of course no.
Please take a look at the image below for a more illustrative example.

Java Strings Confusion [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 6 years ago.
String s1 = "abc";
String s2 = "abc";
String s3 = new String("abc");
String s4 = new String("abc");
if (s1 == s2) is giving true
while (s3 == s4) is giving false.
Can somebody give a detailed explanation onto what is String pool, heap, how many objects are created in each line and how many objects created in total.
Why s3==s4 is giving false?
A detailed explanation will be much appreciated.
When you do new String(...), it is evaluated at runtime and hence creates two different instances and you end up getting s3 == s4 as false. Same thing happens when you use StringBuilder and do something like sb.toString() which is again evaluated at runtime.
For Example,
StringBuilder sb = new StringBuilder();
String foo = sb.append("abc").toString();
String bar = new String("abc");
String foobar = "abc";
Here foo, bar and foobar are all different objects and hence foo == bar or foo == foobar will evaluate to false.
On the other hand s1 == s2 returns true because abc one declared, already exists in the String Pool and in this case both the objects points to the same reference in the pool.
String Class in java is defined in java.lang package and it is exactly that, a class and not a primitive like int or boolean.
Strings are developed to offer operations with many characters ans are commmonly used in almost all the Java applications
Some interesting facts about Java and Strings:
String in immutable and final in Java and in this case JVM uses String Pool to store all the String objects.
What are different ways to create String Object?
We can create String object using new operator like any normal java class or we can use double quotes (literal assignment) to create a String object.
There are too several constructors available in String class to get String from char array, byte array, StringBuffer and StringBuilderetc etc.
To your Question:
When we create a String using double quotes, JVM looks in the String pool to find if any other String is stored with same value. If found, it just returns the reference to that String object else it creates a new String object with given value and stores it in the String pool.
When we use new operator, JVM creates the String object but don’t store it into the String Pool. We can use intern() method to store the String object into String pool or return the reference if there is already a String with equal value present in the pool.
So when you do
String s1 = "abc";
String s2 = "abc";
those are checked in the StringPool and since s1 already exist there, s2 will take the same reference, hence, s1 ==s2 is true.
but when you do:
String s3 = new String("abc");
String s4 = new String("abc");
you are using the new operator, therefore the JVM is not checking if there is an string already in the heap, it will just allocate a new space for s4, so is s3==s4 ??? of course no.
Please take a look at the image below for a more illustrative example.

Categories