Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String pool vs Constant pool

Tags:

Let's look at the folloing code snippet:

  String s1 = "Hello";   String s2 = "Hello";  

Both variables refer to the same object due to interning. Since strings are immutable, only one object is created and both refer to the same object.

A constant pool is also something, which holds all the constants (integer, string, etc.) that are declared in a class. It is specific to each class.

 System.out.println("Hello");  // I believe this Hello is different from above. 

Questions:

  1. Does string pool refer to the pool of a constant string object in the constant pool?
  2. If yes, is String pool common throughout the whole application or specific to a class?
like image 979
Jeevi Avatar asked Apr 23 '14 18:04

Jeevi


People also ask

What is a string constant pool?

A string constant pool is a separate place in the heap memory where the values of all the strings which are defined in the program are stored. When we declare a string, an object of type String is created in the stack, while an instance with the value of the string is created in the heap.

What is the advantage of string pool?

Advantages of String Pool in JavaCache improves performance and reduces memory usage. Provides reusability: It saves time to create a new string if there is already a string with the same value present in the pool. The old string is reused, and its reference is returned.

What is difference between string constant pool and heap memory?

The Java string constant pool is an area in heap memory where Java stores literal string values. The heap is an area of memory used for run-time operations.

What is the string pool?

As the name suggests, String Pool in java is a pool of Strings stored in Java Heap Memory. We know that String is a special class in java and we can create String objects using a new operator as well as providing values in double-quotes.


2 Answers

My questions are,

  1. Does string pool refers to the pool of constant string object in the constant pool?

No.

"Constant pool" refers to a specially formatted collection of bytes in a class file that has meaning to the Java class loader. The "strings" in it are serialized, they are not Java objects. There are also many kinds of constants, not just strings in it.

See Chapter 4.4 the constant pool table

Java Virtual Machine instructions do not rely on the run-time layout of classes, interfaces, class instances, or arrays. Instead, instructions refer to symbolic information in the constant_pool table.

In contrast, the "String pool" is used at runtime (not just during class loading), contains only strings, and the "strings" in the string pool are java objects. The "string pool" is a thread-safe weak-map from java.lang.String instances to java.lang.String instances used to intern strings.

Chapter 3.10.5. String Literals says

A string literal is a reference to an instance of class String (§4.3.1, §4.3.3).

Moreover, a string literal always refers to the same instance of class String. This is because string literals - or, more generally, strings that are the values of constant expressions (§15.28) - are "interned" so as to share unique instances, using the method String.intern.

like image 195
Mike Samuel Avatar answered Sep 19 '22 19:09

Mike Samuel


There is only one string pool, and all string literals are automatically interned.
Also, there are other pools for autoboxing and such.

The constant pool is where those literals are put for the class.

like image 41
Deduplicator Avatar answered Sep 16 '22 19:09

Deduplicator