What is string pool?

Tram Ho

1. What is string pool?

  • A string pool is a special area of ​​memory in the Heap, used to store variables declared as String.
  • String pool helps optimize the storage and memory usage when declaring String variables, helping to limit Java Heap Space overflow.

String at a glance.

  • String is an immutable class. So what is the immutable class? Immutable class is an immutable class, its properties can never be changed and can only be set at initialization.
  • The variable declared as String will be saved to String Pool when created.
  • There are 2 ways to declare variables of type String:

Method 1:

Method 2:

The above two ways seem to be the same but actually not.

2. String pool and how it works.

In the above example:

Method 1:

  • When declared by 1: Java will access the String Pool, then search in the Pool which memory cell has the same value as it, if found, will refer to the address of that memory cell, otherwise it will Create a new memory cell in the Pool and then perform the reference.
  • Declare the variable String s1 = “Cat”, it will create a new memory cell in the String Pool with the value “Cat” because at this time, there is no memory of that value in the Pool.
  • At declaration String s2 = “Cat”; it finds in the Pool the value of a memory cell with the value “Cat”, s2 simply refers to the address of this memory cell. Thus s2 has the value of “Cat” thanks to the reference without having to create another memory cell.
  • Because s1 and s2 refer to the same heap memory address of the candle s1 == s2

Method 2:

  • When declaring variant 2: Java will not create a new cell in String Pool memory but will create in Java Heap Space. And then it will always create new memory even if other available cells have the same value.
  • String s3 = new String (“Cat”), this line will create a memory cell in Java Heap Space with value “Cat”, this s3 variable will reference the address of that memory cell.
  • And if any: String s4 = new String (“Cat”), similarly this line will create another memory cell in Java Heap Space also has the value of “Cat” (unlike the above, which will find the memory cell that has Such a value then refers to it), this s4 variable will refer to the address of the new memory cell.
  • And because s3 and s4 don’t refer to the same memory address in the heap, s3! = S4

3. Compare String

  • There are two ways to compare strings: using the == operator and using the equals () method .
  • The == operator compares the reference of an object, the similarity in memory. Therefore, if two string a and b objects refer to the same literal in the string pool, or the same reference to an object in the heap, then a == b will return true. Otherwise, will return false
  • The equals () method is overridden in the String class. It checks the value of the string stored in the string object. Therefore, if a and b contain the same string of characters, a.equals (b) always returns true, no matter where they refer to.
Share the news now

Source : Viblo