| 

.NET C# Java Javascript Exception

Antwort #A1485 zur Frage #F396: Warum sind Strings immutable?

Diese Antwort hat bisher 2 Versionen. Frage #F396: Warum sind Strings immutable? - Antwort #A1485


Version 2
28.10.2009 08:51:09
Dies ist die aktuelle Version
public class IchbinString{
public static void main(String args[]){
String s1 = new String("Hallo");
String s2 = new String("Du");
System.out.println(s1);
s1=s2;
System.out.println(s1);
}
}


Schau dir den Code mal an. Wenn Strings wirklich unveränderlich wären, dann sollte s1 "Hallo" ausgeben, aber wie du siehst gibt das zweite Output "Du" aus... Also sind Strings nicht so wirklich immutable ;)
Forlan 177 5
Version 1
28.10.2009 08:51:09
public class IchbinString{
public static void main(String args[]){
String s1 = new String("Hallo");
String s2 = new String("Du");
System.out.println(s1);
s1=s2;
System.out.println(s1);
}
}


Schau dir den Code mal an. Wenn Strings wirklich unveränderlich wären, dann sollte s1 "Hallo" ausgeben, aber wie du siehst gibt das zweite Output "Da" aus... Also sind Strings nicht so wirklich immutable ;)
Forlan 177 5