I am not able to understand usage of intern
method provider by String class.
String s1 = "vivek"; //Stmt 1
String s2 = "vivek"; //Stmt 2
String s3 = s1.intern(); //Stmt 3
System.out.println(s1 == s2);// prints true
System.out.println(s1 == s3);// prints true
I read that internally compiler do an intern, but on which object? How do compiler knows he had to do intern for s1
to create s2
?
What is the difference between Stmt 2
and Stmt 3
here? Are both same always? If yes, what is so special about intern
method of java?
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.
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 if not already present. If the string is already present, it returns the reference. The intern() method helps to save memory space and reuse it efficiently at the cost of time.
The Java String class intern() method returns the interned string. It returns the canonical representation of string. It can be used to return string from memory if it is created by a new keyword. It creates an exact copy of the heap string object in the String Constant Pool.
In computer science, string interning is a method of storing only one copy of each distinct string value, which must be immutable. Interning strings makes some string processing tasks more time- or space-efficient at the cost of requiring more time when the string is created or interned.
The difference is that the initialization way of the variable that decides where to save the variable ;
String Interning Oracle reference .
There are two ways to construct a string: implicit construction by assigning a string literal or explicitly creating a String object via the new operator and constructor. For example
String s1 = "Hello"; // String literal
String s2 = "Hello"; // String literal
String s3 = s1; // same reference
String s4 = new String("Hello"); // String object
String s5 = new String("Hello"); // String object
Java has provided a special mechanism for keeping the String literals - in a so-called string common pool. If two string literals have the same contents, they will share the same storage inside the common pool. This approach is adopted to conserve storage for frequently-used strings. On the other hand, String objects created via the new operator and constructor are kept in the heap. Each String object in the heap has its own storage just like any other object .
s1 == s1; // true, same pointer
s1 == s2; // true, s1 and s1 share storage in common pool
s1 == s3; // true, s3 is assigned same pointer as s1
s1.equals(s3); // true, same contents
s1 == s4; // false, different pointers
s1.equals(s4); // true, same contents
s4 == s5; // false, different pointers in heap
s4.equals(s5); // true, same contents
Important Notes:
since when you create a string like : String s1="vivek"; this string is created in the string constant pool, and a reference is returned to "s1".
When you are using intern method to create a string like: s3=s1.intern(); This intern method returns the reference from the string constant pool only. Thats why both string are same as both contains reference to same string object.
refer the below image:
But if u create a string like :
String s1=new String("vivek"); //---here one "vivek" will be stored in string constant pool and second "vivek" will be stored in heap area and "s1" contains reference to string in heap area
String s2="vivek";
String s3=s1.intern();//returns string from pool, now it will be same as s2
System.out.println(s1==s2);//false because reference is different
System.out.println(s2==s3);//true because reference is same
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