Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does String.intern() work and how does it affect the String pool?

Tags:

java

string

As we know, String().intern() method add String value in string pool if it's not already exist. If exists, it returns the reference of that value/object.

String str = "Cat"; // creates new object in string pool  with same character sequence.  
String st1 = "Cat"; // has same reference of object in pool, just created in case of 'str'

 str == str1 //that's returns true

String test = new String("dog");
test.intern();// what this line of code do behind the scene

I need to know, when i call test.intern() what this intern method will do?

add "dog" with different reference in string pool or add test object reference in string pool(i think, it's not the case )?

I tried this

String test1 = "dog";

test == test1 // returns false

I just want to make sure, when I call test.intern() it creates new object with same value in String pool? now I have 2 objects with value "dog". one exist directly in heap and other is in String pool?

like image 467
Muneeb Nasir Avatar asked Jun 19 '15 07:06

Muneeb Nasir


People also ask

How does string intern work?

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 pool and what is the use of intern () function?

All Strings are stored in the String Pool (or String Intern Pool) that is allocated in the Java heap. String pool is an implementation of the String Interring Concept. String Interning is a method that stores only a copy of each distinct string literal. The distinct values are stored in the String pool.

How does string work in string pool?

Each time a string literal is created, the JVM checks the string literal pool first. If the string already exists in the string pool, a reference to the pooled instance returns. If the string does not exist in the pool, a new String object initializes and is placed in the pool.

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.


1 Answers

when i call test.intern() what this intern method will do?

It will put the "dog" string in the string pool (unless it's already there). But it will not necessarily put the object that test refers to in the pool. This is why you typically do

test = test.intern();

Note that if you have a "dog" literal in your code, then there will already be a "dog" in the string pool, so test.intern() will return that object.

Perhaps your experiment confuses you, and it was in fact the following experiment you had in mind:

String s1 = "dog";             // "dog" object from string pool
String s2 = new String("dog"); // new "dog" object on heap

System.out.println(s1 == s2);  // false

s2 = s2.intern();              // intern() returns the object from string pool

System.out.println(s1 == s2);  // true
like image 100
aioobe Avatar answered Oct 13 '22 00:10

aioobe