Immutable nature of string is charecterised by it’s property that one’s a memory location is reserved (allocated) to a string and if string value changes then old value is not over written by new one.
Lets discuss it,
For example : Create an instance of type String as s1.
It will reserve a memory location in Heap Memory with s1 storing the address of memory location as shown,
Now let’s change the content of s1 ,in case of primitive data type like int, float , char etc .If we change the containt of one variable the privious value is overwritten by the new value, but in case of String type instances this is not possible ,here a new memeory location is created which will point to the new value as shown..
So new memory location is created for storing new value (new string)
This behavior of String Instances is reffered as Immutable nature of String.
Leave a Reply to PralayCancel reply