In different phrases, the string fixed pool exists primarily to cut back reminiscence utilization and enhance the reuse of present cases in reminiscence. There are some circumstances the place we would like a definite String object to be created regardless that it has the identical worth. On this case, we use the new key phrase.
Allow us to now perceive with a fundamental instance, how strings are saved in reminiscence.
Instance 1: On this instance, we are going to create Strings and perceive how they’re saved in reminiscence.
Beneath is the diagrammatic illustration of how the strings within the above instance shall be saved in reminiscence.
We will see that str1 and str2 level on the similar location within the reminiscence whereas a brand new area is created for str3 because it has a special worth. On this approach string fixed pool saves reminiscence by making the identical worth string level to the identical location within the reminiscence.
Instance 2: On this instance, we are going to make the strings with the identical worth discuss with completely different areas in reminiscence
We will see within the picture that regardless that str1 and str2 are having the identical worth however due to the brand new key phrase they’re referring to completely different areas within the reminiscence. Therefore, they return false compared.
When creating the strings utilizing quotations(” “) they’re instantly saved within the String Fixed Pool the place equal values discuss with the identical location within the reminiscence. Whereas, when strings are created utilizing the brand new key phrase, a brand new occasion is at all times created within the heap reminiscence then the worth is saved within the String Fixed Pool due to this even when the info saved is similar nonetheless the strings won’t be equal.