Have a Question?

If you have any question you can ask below or enter what you are looking for!

What is the importance of SCP(String constant pool)?


What is the importance of SCP(String constant pool)?

There are two ways to create a String object in Java:

  • Using the new operator. For example,
    String str = new String("Java4us");.
  • Using a string literal or constant expression). For example,
    String str="Java4us"; (string literal) or
    String str="Java" + "4us"; (string constant expression).
  • In our program, if a String object is repeatedly required then it’s not recommended to create a separate object for every requirement because it creates performance and memory problems.
  • Instead of creating a separate object for every requirement we have to create only one object and we can reuse the same object for every requirement so that performance and memory utilization will be improved.
  • This thing is possible because of SCP. Hence the main advantages of SCP are memory utilization and performance will be improved.
  • But the main problem with SCP is, as several reference pointing to the same object, by using one reference if we are trying to change the content then remaining reference will be affected.
  • To overcome this problem SUN People implemented String object as immutable. That is once we create a String object we can’t perform any change in the existing object. If we are trying to perform any change with those change a new object will be created.
  • Hence SCP is the only reason for immutability of string objects.
  • We know String is the most popular type present in Java and almost all Java programs use it. In fact, I have not seen a single Java program which is written without using String.
  • In general, a normal Java business application deals with thousands of string objects, lots of them have same value associated and lots of them are mid operations string means they are not the final result.
  • So if we store all those string objects in normal heap memory, lot’s of the heap size will be acquired by just string objects only, and the garbage collector will have to run more frequently which will decrease the performance of the application.
  • And that’s why we have String Constant Pool and String interning process, whenever we create a string literal JVM first sees if that literal is already present in the constant pool or not and if it is there, the new variable will start pointing to the same object, this process is called String Interning.
  • String allocation, like all object allocation, proves costly in both time and memory. The JVM performs some trickery while instantiating string literals to increase performance and decrease memory overhead. To cut down the number of String objects created in the JVM, the String class keeps a pool of strings. Each time your code create a string literal, the JVM checks the string literal pool first. If the string already exists in the pool, a reference to the pooled instance returns. If the string does not exist in the pool, a new String object instantiates, then is placed in the pool. Java can make this optimization since strings are immutable and can be shared without fear of data corruption.


Tags:  , , , , , ,

Leave a Reply

Your email address will not be published.