Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why jvm create new string Object each time we create string using new keyword

If jvm creates string pool for memory optimization, then why it creates new Object each time we create string using new keyword even though it exists in string pool?

like image 548
Harsh Patel Avatar asked Sep 23 '15 07:09

Harsh Patel


4 Answers

... why does Java create new Object each time we create a string using the new keyword even though it exists in string pool?

Because you explicitly told it to! The new operator always creates a new object. JLS 15.9.4 says:

"The value of a class instance creation expression is a reference to the newly created object of the specified class. Every time the expression is evaluated, a fresh object is created."


For the record, it is nearly always a mistake to call new String(String) ... but in obscure cases it might be useful. It is conceivable that you might want a string for which equals returns true and == gives false. Calling new String(String) will give you that.


For older versions of Java, the substring, trim and possibly other String methods would give you a string that shared backing storage with the original. Under certain circumstances, this could result in a memory leak. Calling new String(str.trim()) for example would prevent that memory leak, at the cost of creating a fresh copy of the trimmed string. The String(String) constructor guarantees to allocate a fresh backing array as well as giving you a new String object.

This behavior of substring and trim changed in Java 7.

like image 104
Stephen C Avatar answered Oct 20 '22 19:10

Stephen C


To give primitive style of declaration and for performance designers introduced String literals.

But when you use new keyword, then you are explicitly creating objects on heap not in constant pool.

When the objects created on heap, there is no way to share that memory with each other and they become completely strangers unlike in constant pool.

To break this barrier between heap and constant pool String interning will help you out.

string interning is a method of storing only one copy of each distinct string value, which must be immutable

Remember that constant pool also a small part of heap with some additional benefits where sharing of memory is available.

like image 44
Suresh Atta Avatar answered Oct 20 '22 19:10

Suresh Atta


When you write

String str = new String("mystring"); 

then it creates a string object in heap just like other object which you create. The string literal "mystring" is stored in the string constant pool.

From the Javadocs:

A pool of strings, initially empty, is maintained privately by the class String.

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.

It follows that for any two strings s and t, s.intern() == t.intern() is true if and only if s.equals(t) is true.

like image 44
Rahul Tripathi Avatar answered Oct 20 '22 20:10

Rahul Tripathi


To take advantage of string pooling you need to use String#intern instead of new.

like image 38
marthursson Avatar answered Oct 20 '22 21:10

marthursson