Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java String literal pool and string object

Tags:

java

string

I have known that JVM maintains a string literal pool to increase performance and maintain JVM memory and learned that string literal is maintained in the string pool. But I want to clarify something related to the string pool and string object created on the heap.

Please correct me if my explanation is wrong.

String s = "abc";

If the above line is executed, "abc" string literal is added to the string pool if it does not exist in the pool. And string object is created on the heap and a reference s will point to the literal in the pool.

Questions:

  1. Does this code create string object on the heap every time it is executed?
  2. Does string literal pool maintain only string literals or does it maintain string object as well?
  3. When does JVM decide that it needs to add string literal to the string pool? does it decide in the compile time or runtime?

I am not sure where exactly string object is created if it points to a string literal in the pool.

Thanks.

like image 702
user826323 Avatar asked Nov 08 '11 04:11

user826323


People also ask

What is difference between string literal and String object in Java?

The main difference between String Literal and String Object is that String Literal is a String created using double quotes while String Object is a String created using the new() operator. String is a set of characters. Generally, it is necessary to perform String operations in most applications.

What is String and String pool in Java?

What is String Pool in Java? String Pool is a storage area in Java heap. String allocation, like all object allocation, proves to be a costly affair in both the cases of time and memory. The JVM performs some steps while initializing string literals to increase performance and decrease memory overhead.

Is string literal an object in Java?

In Java, a String is an Object . Strings should not be confused with char as characters are literally 1 value rather than a sequence of characters. You can still use 1 value within a String, however it is preferred to use char when you are checking for 1 character.

What is the String pool in Java What is the difference in String pool between Java 6 and 7?

In summary here, is the important difference in String pool in Java 6 and 7: String pool is relocated to Java heap space from PermGen space. The default size of String pool is increased to 600013 entries from 1009 in Java 6. The -XX:StringTableSize JVM option is provided to specify the size of the String pool.


2 Answers

Quoting documentation for String.intern()(emphasis mine)

All literal strings and string-valued constant expressions are interned. String literals are defined in §3.10.5 of the Java Language Specification


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.


Thus,

Does this code create string object on the heap every time it is executed?

Only one object is created for each unique interned string. All references share this immutable object.

Does string literal pool maintain only string literals or does it maintain string object as well?

There are no 'Literal Objects'. Literal string expressions when converted, are stored as regular String objects.Also, the pool contains all interned string objects. Both implicit (by using a string literal expression) and explicit (by calling .intern() on a String object).

When does JVM decide that it needs to add string literal to the string pool? does it decide in the compile time or runtime?

I'm not sure.

like image 67
Sahil Muthoo Avatar answered Oct 07 '22 02:10

Sahil Muthoo


There is no "literal pool". Interned Strings are just normal heap objects. They may end up in the PermGen, but even then, they could eventually be garbage-collected.

The class file has a constant pool, which contains the String literals used in the class. When the class is loaded, String objects are created from that data, which is probably very similar to what String#intern does.

Does this code create string object on the heap every time it is executed?

No. There will be one String object that is being reused. It has been created when the class was loaded.

Does string literal pool maintain only string literals or does it maintain string object as well?

You can intern Strings as well. I assume that they are treated more or less the same.

When does JVM decide that it needs to add string literal to the string pool? does it decide in the compile time or runtime?

Literals are always "pooled". Other Strings need to have "intern" called on them. So in a way, the decision is made at compile-time.

like image 5
Thilo Avatar answered Oct 07 '22 03:10

Thilo