This is a most common question generally asked during interviews, i will not be going into details but just showing some examples that might help in understanding string pool and integer pool in a better way, or probably assist you in remembering the concept so that you would be able to answer it correctly.

What is a string pool?

  • Simply, a pool of strings stored in the Java Heap Memory.

Why does Java use the string pool concept?

  • For memory optimization. So, if a copy of the string already exists, it will not create a new String to hold the repeated value, but rather point to the address that has the same value as the string you are planning to assign.

What is an integer pool? 

  • Java tends to cache frequently used integers in a pool (but note, this only applies to a certain range of numbers)

Why does java use an integer pool?

  • To improve performance while processing integers, case like, auto-boxing integer values (Integer intValue = 5)

Let’s look at an example:

Screen Shot 2018-03-21 at 6.59.35 PM.png

The output of the above is as under:

5
5
false
true
true
true
5
5
false
true

You will notice that: 

comparing and c returns true, because both of them point to the same reference. While the others that return false have references that are different. new Integer() literally creates a new object and thus assigned a different address, while Integer.valueOf() does not create a new object if it lies within a certain range. It is thus, advised to use valueOf instead of using new Integer() to prevent un-necessary creation of objects.

Integer stores the values from -128 to 127 in a cache, and hence uses the  pool concept, but any value greater than 127, a new object will be created, thus for the following example, the result returned will always be false.

Screen Shot 2019-02-06 at 4.04.10 PM.png

While in the cases of Strings, even though string are immutable and you try to assign a string value to a string object, a new instance is not created. Rather, it will check in its pool if the string exists. If it does, the new string points to the reference, else a new instance in created, and an address is assigned to this new string.

For instance: When strA is being assigned the value 5, there is a check as to whether the value exists or not in the pool. If not, 5 is added to the pool and strA would point to the address allocated by 5. strB is created as a new object entirely, so it will be assigned a fresh address. strC ideally, on the basis of immutability should lead to the creation of a new instance, but based on String pool concept, it points to the same address as strA, because the value exists.

You can read more of this from the links below (and there are many more resources out there)

  1. https://stackoverflow.com/questions/13098143/why-does-the-behavior-of-the-integer-constant-pool-change-at-127
  2. https://coderanch.com/t/601317/java/Integer-Pool
  3. https://puredanger.github.io/tech.puredanger.com/2007/02/01/valueof/
  4. https://stackoverflow.com/questions/2486191/what-is-the-java-string-pool-and-how-is-s-different-from-new-strings

 

Advertisements