Created
June 3, 2015 14:58
-
-
Save cangoal/49defe95b1fe1bfb5815 to your computer and use it in GitHub Desktop.
Understand the "pass by value" in Java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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