How a string is immutable | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How a string is immutable

Bcoz we can still modify a string e.g."pine"+"apple"=pineapple Here the string pine is modified.... So plz explain

15th Mar 2017, 5:32 PM
PREETAM
PREETAM - avatar
3 Answers
+ 7
the string is not modified but rather a new stri g is made and is referenced 1) x="hello" 2) y="world" 3) x=x+" "+y what happens above is that on line 3 we assign a new concatanated string to x, destroying what was there before. so we are not actually adding, but assigning something new constructed from the old
15th Mar 2017, 6:01 PM
Burey
Burey - avatar
+ 4
A String is an object of the string class. your addition operator is only an abstraction to indicate the creation of a new String object that will hod the data resulting from the concatenation of two other objects.
15th Mar 2017, 6:20 PM
seamiki
seamiki - avatar
+ 2
Here pineapple is a new string. eg. 2+3=5 here 2 is not modified into 5.
15th Mar 2017, 5:42 PM
Mohit Khokhar
Mohit Khokhar - avatar