Immutable Strings and String Pool - ayushmathur94/DirectQuesAns_Prep GitHub Wiki

 String str = "knowledge"; 

This creates a string literal containing "knowledge" and assigns it a reference str.

 String s = str; 

//assigns a new reference to the same string "knowledge"

 str = str.concat(" base");  

This appends " base" to str.

HOW IS IT POSSIBLE SINCE STRING OBJECTS ARE IMMUTABLE ?

when the above statement is executed, the JVM takes value of String str i.e. "knowledge" and appends " base" , giving us the value "knowledge base" . Now since strings are immutable , the JVM can't assign this value to str, so it creates a new String object , gives it a value "knowledge base" and gives it a reference str.

An important point to note here is that, while the String objects is immutable, its reference variables is not. So, that's why in above example the reference was made to refer a newly formed String Object.

 import java.io.*;
  
class GFG {
    public static void main(String[] args)
    {
        String s1 = "java";
        s1.concat(" rules");
  
        // Yes, s1 still refers to "java"
        System.out.println("s1 refers to " + s1);
    }
}

Output: 
s1 refers to java

What’s happening:

The first line is pretty straightforward: create a new String "java" and refer s1 to it. Next, the VM creates another new String "java rules", but nothing refers to it. So, the second String is instantly lost. We can’t reach it. The reference variable s1 still refers to the original String "java".

stringpool

When the compiler sees a String literal, it looks for the String in the pool. If a match is found, the reference to the new literal is directed to the existing String and no new String object is created. The existing String simply has one more reference.
Here comes the point of making String objects immutable:
In the String constant pool, a String object is likely to have one or many references. If several references point to same String without even knowing it, it would be bad if one of the references modified that String value. That’s why String objects are immutable.

Well, now you could say, what if someone overrides the functionality of String class? That’s the reason that the String class is marked final so that nobody can override the behavior of its methods.

string

A String Pool is a memory area where all string literals are cached by default. So whenever you create a new string literal, it first checks whether it is present in the String Pool. If yes, it returns the reference otherwise, it creates a new one in String Pool, and then the reference of that new one is returned.

Whenever you create a string using a new operator, the String object is created in the Heap Memory. But you can add that String object in the String Pool by using the intern method.

Prior to Java 7, the JVM placed the Java String Pool in the PermGen Space which has a fixed size — it can’t be expanded at runtime and it is not eligible for garbage collection. So, it can easily go out of memory if you create too many string literals or if you intern too many string objects. To avoid this, the String Pool was moved to the heap space from JDK 7 which is now eligible for garbage collection. The advantage here is that it reduces the chances of out of memory error as unreferenced strings are garbage collected.

⚠️ **GitHub.com Fallback** ⚠️