Skip to content

Instantly share code, notes, and snippets.

@cangoal
Created June 3, 2015 14:58
Show Gist options
  • Save cangoal/49defe95b1fe1bfb5815 to your computer and use it in GitHub Desktop.
Save cangoal/49defe95b1fe1bfb5815 to your computer and use it in GitHub Desktop.
Understand the "pass by value" in Java
// http://www.programcreek.com/2013/09/string-is-passed-by-reference-in-java/
public static void test (){
ArrayList<StringBuilder> ret = new ArrayList<StringBuilder>();
StringBuilder sb = new StringBuilder("abc");
ret.add(sb);
sb.append("d"); // sb = new StringBuilder("cba")
System.out.println(ret.toString());
ArrayList<String> lst = new ArrayList<String>();
String str = "abc";
lst.add(str);
str += "d"; // str = "cba"
System.out.println(lst.toString());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment