Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




Java - Strings in Free Pool


How are String values managed in free pool?


When we define a String type variable of name firstString by assigning a value Robert to it,


String firstString = "Robert";

A memory space will be created for firstString in so called free pool and the value Robert will be stored in it.


Now, if we define another String type variable secondString and assign the same value Robert to it,


String secondString = "Robert";
  1. Java searches in the free pool if the value 'Robert' is present of not.


  2. If Robert is already present in the 'free pool'. A new memory space won't be created for 'secondString'.


  3. Rather, 'secondString' will also be pointing to the same memory location which 'firstString' is pointing to.


    Spring_Boot