String

String class is immutable. Immutable means once String object created then any modification is not permitted in that object. So once created, can not be changed.
Once String object is created then that object will be added in the String Literal Pool. If String object is created in the Pool then again with the same String object won’t be created. If developer will try to modify the existing String object then existing object will be abandoned and old string will be non trackable.

Note: Keep in mind, only one pool possible for each class.

string in corejava
Some key points for above demo
  1. In section-1
    String s1 = “Technical”;Since s1 is the first String object so there are no existing String object in Pool. So new String object “Techinical” will be created in Heap and added in the pool.
  2. In section-2 
    String s2 = “Jungle”;
    New String object “Jungle” will be created in Heap and added in the pool.
    String s3 = s2;Now s3 will also start pointing existing String object “Jungle” which in already in pool.
    string in core java
Some key points for above demo 
  1. In section-3
    String s4 = s1 + s2;Already in pool, we have
    s1 = Technical
    s2 = s3 = Jungle
    s4 = TechnicalJungle

    S4 will be added to String Literal Pool.

  2. In section-4
    String s4 = s4 + “.com”;Already in pool, we have 
    s1 = Technical
    s2 = s3 = Jungle
    s4 = TechnicalJungle

    After that as per execution:
    s4 = TechnicalJungle.com

    TechnicalJungle.com” has been assigned to s4 and existing “TechnicalJungle” will become abondoned.

  3. Once any reference is pointing some object and new object will be assigned then automatically the older object will be abandoned. It means that string object won’t be able to be tracked. 

Some key points for above demo
  1. Str1 = “TechnicalJungle.com” String object has been created and added to String Literal Pool. 
  2. When statement Str2= “TechnicalJungle.com” has been exexuted, then JVN will try to search “TechnicalJungle.com” in the pool. Its lucky this time. FOUND STRING OBJECT IN THE POOL. So now str2 will also start referencing existing string obejct “TechnicalJungle.com”.
  3. String str3 = new String(“TechnicalJungle.com”);
    Here “new” operator has been used.
    Two object will be created.One will be added in to Non-Literal Pool which is referenced by str3. Other object will created in String Literal Pool but it is abandoned, means no reference can access it.

  4. Since str1 and str2 both are pointing to same object. So its hashcode and content everything will be same. str1 == str2.
  5. Since str1 and str2 both are at different location. As I already have explained str3 is in Non-Literal Pool.Hence str1 not equal to str3. so (str == str3) –> false
    But its content is same so so (str.equals(str3)) –> true

If you like the post then please socialize it. Happy Sharing. Loudly Subscribing.

One Comment

  1. Pingback: String vs StringBuffer vs StringBuilder - Technical Jungle

Leave a Reply

Your email address will not be published. Required fields are marked *