Where is the String object built using the new one separate from the heap?

Is a String created using the new operator on the heap as well as in the string pool?

Or maybe someone can point me to some document or link that will guide me in relation to the string pool?

0
java string
Apr 02 '13 at 10:02
source share
4 answers

Case 1 -

String s1="world"; String s2="india"; String s3="world"; 

here two objects will be created in the pool. s1 and s3 point to the same object. string literals go to pool memory.

Case 2 -

 String s1=new String("world"); String s2=new String("india"); String s3=new String("world"); 

here three objects will be created on the heap.

the new word uses heap memory to create objects.

I hope I could say.

see link for a more detailed understanding.

+1
Apr 02 '13 at
source share
β€” -

The string literals in your Java code are in the String pool, but are created using the new one.

A row pool is a cache that is also stored on the heap.

+2
Apr 02 '13 at
source share

A string created using the new operator is not in the String pool in Heap (the string pool is also present in the heap memory). But you can move this object to the pool using String.intern() .
After calling String.intern() on a String, if a string created using the new keyword is already in the pool, a link to the merged object will be returned. Now it will point to the merged object instead of the previous one. More details

0
Apr 02 '13 at 10:26
source share

Nothing really promised.

The compiler / JVM may decide to create it in the String pool, but also in neap. Today, he can create you Strings in the String literal pool, tomorrow after updating the JVM, he can create them on the heap for exactly the same code.

The main thing is that your code should not depend on where your lines are created.

For example, the escape == operator to compare String values.

0
Apr 02 '13 at 15:59
source share



All Articles