Skip to content

Instantly share code, notes, and snippets.

@lukaspili
Created April 23, 2012 18:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lukaspili/2472910 to your computer and use it in GitHub Desktop.
Save lukaspili/2472910 to your computer and use it in GitHub Desktop.
public class StringImmuabilityTraining {
public static void main(String[] args) {
// String are immuable
String foo = "John";
String foo2 = foo;
foo += "Toto";
System.out.println(foo); // "JohnToto"
System.out.println(foo2); // "John"
// StringBuilder are muable
StringBuilder sb = new StringBuilder("John");
StringBuilder sb2 = sb;
sb.append("Toto");
System.out.println(sb2); // "JohnToto"
System.out.println(sb2); // "JohnToto"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment