Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between String intern method and normal string creation [duplicate]

Tags:

java

string

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?

like image 451
Vivek Vardhan Avatar asked Apr 07 '18 14:04

Vivek Vardhan


People also ask

What is String intern () method used for?

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 the use of the intern () method it creates a new string in database?

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.

What is the use of the intern () method hard?

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.

What is interning a string?

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.


Video Answer


2 Answers

The difference is that the initialization way of the variable that decides where to save the variable ;

  1. if it has the same value and same initialization method and initialized using new keyword - it will save it in heap and will save each variable as new object even if it has same value.
  2. if it has the same value and same initialization method and initialized directly - it will reference it in JVM pooled memory .

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 .

enter image description here

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:

  1. In the above example, I used relational equality operator '==' to compare the references of two String objects. This is done to demonstrate the differences between string literals sharing storage in the common pool and String objects created in the heap. It is a logical error to use (str1 == str2) in your program to compare the contents of two Strings.
  2. String can be created by directly assigning a String literal which is shared in a common pool. It is uncommon and not recommended to use the new operator to construct a String object in the heap.
like image 113
Mohammad Avatar answered Sep 21 '22 17:09

Mohammad


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: enter image description here

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  
like image 32
akash verma Avatar answered Sep 19 '22 17:09

akash verma