String immutability in java

The following two tabs change content below.
Prasad Kharkar is a java enthusiast and always keen to explore and learn java technologies. He is SCJP,OCPWCD, OCEJPAD and aspires to be java architect.

Latest posts by Prasad Kharkar (see all)

Hello all, In this tutorial we are going to learn something interesting about strings in java. The String immutability
So what is it exactly? Instead of learning this part theoretically, I will write a program and you have to guess the output.

You would say, “Duh! Are you kidding man? This is a simple string concatenation program, whats the big deal? The output should be ‘prasadkharkar‘”. Well are you sure? Let us run this program and the output I get by running this is prasad

Hmmm…What happened here? we have called the concat method on s1 right? so why doesn’t it output “prasadkharkar“?.
Have we done what is necessary to output the concatenated string? Let us understand what is happening diagrammatically when we are declaring and initializing variables.

Two objects are created

Two objects are created

A reference variable s1 is created and the string object “prasad” is assigned to it.
A reference variable s2 is created and the string object “kharkar” is assigned to it.
Now all the action goes when we are calling s1.concat(s2) .

  • When you call s1.concat(s2) then a new string object will be created that is the result of concatenation, and the value of object will be prasadkharkar
  • Note that this does not alter the “prasad” object. It simply creates a new “prasadkharkar“.
  • But after “prasadkharkar” is created, it is lost to our program as we are not referring to it by any variable. So the concatenation will happen but it will be lost to our program.

This is depicted in the image below


New String is created

New String is created

Now we will change  s1.concat(s2)  to s1 = s1.concat(s2) . and re-run the program.
This will output prasadkharkar.

  • s1.concat(s2)  creates a new object on the heap
  • s1 = s1.concat(s2)  assigns the object to the reference variable s1
  • Now s1 is referring to the “prasadkharkar” so its link to “prasad” is broken.
Object References Changed

Object References Changed

In short, when you are performing some operations on string objects, then new string objects are created and the original strings remain unaffected. i.e. original string objects cannot be changed. This phenomenon is called as immutability of strings. But the reference variables are not immutable. You can assign the same reference variable to multiple objects.

Hope this helps you understand the concept of string immutability.


Share Button

24 comments for “String immutability in java

  1. July 2, 2013 at 11:36 am

    Nice blog. Very informative.

  2. August 28, 2013 at 2:43 pm

    String immutability in java | theJavaGeek

  3. Sriram
    September 27, 2013 at 8:47 am

    Images are not being displayed.

    • Prasad Kharkar
      September 30, 2013 at 11:52 pm

      sriram, The problem could be with your network. I am not able to find any problem in viewing images.

  4. Sudhir Kumar
    November 22, 2013 at 5:59 am

    yes image is not displaying, i tried in both mozilla n crome .

    • Prasad Kharkar
      November 22, 2013 at 10:49 am

      Hi Sudhir, I will look into the problem and solve as soon as possible.

  5. Loralee
    February 11, 2014 at 6:26 pm

    Do you mind if I quote a few of your articles as
    long as I provide credit and sources back to your weblog?
    Please let me know if this
    okay with you. Many thanks!

    • Prasad Kharkar
      February 11, 2014 at 7:01 pm

      Yes, you can.

  6. Pankaj Arora
    November 24, 2014 at 6:06 pm

    Awesome Explaination, hats off!!

  7. vixir
    December 28, 2014 at 2:12 pm

    So good.

  8. sushil
    March 30, 2015 at 2:26 am

    good explanation

  9. Seeta
    May 10, 2015 at 10:17 am

    What happens to “Prasad” object which was referenced by s1 after reassigning s1 to “prasadkharkar”. Will it be automatically garbage collected? How long it will stay in the heap space?

    • May 15, 2015 at 10:01 am

      It will be eligible for garbage collection as it does not have any references accessing it. When it will be garbage collected is completely based upon garbage collection algorithm.

  10. ishan
    June 30, 2015 at 1:57 am

    what happen when we write
    string s=”ishan”;

  11. Sagarika Sahoo
    August 21, 2015 at 8:00 pm

    Nice explanation sir, now I understood why Strings are immutable in java. Thank you.

  12. March 14, 2016 at 5:30 pm

    Very Well explained sir and just put in the right way. I referred to may blogs and was still wondering if i assign s1 = s1.concat(s2) will change s1. so how do we say that string is immutable. But your explanation that “String objects are immutable, but references variables are mutable” resolved my doubt. Thank you so much.

    • March 15, 2016 at 8:12 am

      I am glad it was helpful for you. Keep giving feedback. It will help me improve myself.

  13. Neeraja
    May 11, 2016 at 1:21 pm

    Had read many blogs before explaing String immutability.None of them was so informative.
    Nice.Keep up the good work

    • May 12, 2016 at 8:20 am

      Thank you Neeraja for those words. It means a lot to me. I will definitely keep contributing. Happy learning 🙂

  14. Gowtham
    December 12, 2016 at 12:38 pm

    Really great explanation.thanks a lot

  15. fajri
    May 8, 2017 at 2:06 am

    ofcourse we will get output “prasad”
    cause u just execute s1.concat(s2);

    try compile and run this :
    public class StringImmutability{
    public static void main(String [] args){
    String s1 = new String(“prasad”);
    String s2 = new String(“kharkar”);
    s1 = s1.concat(s2);


    public class StringImmutability{
    public static void main(String [] args){
    String s1 = new String(“prasad”);
    String s2 = new String(“kharkar”);

  16. Mounika
    June 20, 2017 at 6:43 pm

    You can assign the same reference variable to multiple objects.
    But you can store the reference of recently assigned object in that reference variable
    .Is it right?

  17. Rajan Chauhan
    July 19, 2017 at 8:30 pm

    well i was looking for such kind of explanation and after a long day struggle landed here , thanks a lot 🙂

Leave a Reply

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