string s1 = "hello"; string s2 = s1; s1 = "world"; Console.WriteLine(s2);
In C#, strings are immutable and variables hold references to string objects. When s2 = s1 is done, both point to "hello". Changing s1 to "world" makes s1 point to a new string, but s2 still points to "hello".
let s1 = "hello"; let s2 = s1; s1 = "world"; console.log(s2);
JavaScript strings are immutable. Assigning s2 = s1 copies the reference to the string "hello". Changing s1 to "world" does not affect s2.
s1 = "hello" s2 = s1 s1 = "world" print(s2)
In Python, strings are immutable. Assigning s2 = s1 makes both variables point to the same string "hello". Changing s1 to "world" points s1 to a new string, but s2 remains "hello".
String s1 = "hello"; String s2 = s1; s1 = "world"; System.out.println(s2);
Java strings are immutable. Assigning s2 = s1 makes both variables point to "hello". Changing s1 to "world" points s1 to a new string, but s2 still points to "hello".
Immutability allows strings to be shared safely across different parts of a program without risk of one part changing the string unexpectedly for others. This helps with thread safety and caching.
