Skip to content

Instantly share code, notes, and snippets.

@snicmakino
Last active November 16, 2017 12:36
Show Gist options
  • Save snicmakino/d51322220c79ed82e7d76c1fe71936a6 to your computer and use it in GitHub Desktop.
Save snicmakino/d51322220c79ed82e7d76c1fe71936a6 to your computer and use it in GitHub Desktop.
Stringがイミュータブルである事の簡易的な説明用
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) throws Exception {
Main main = new Main();
main.exec();
main.exec2();
}
// イミュータブル
void exec() {
String str = "string!!";
this.strToNull(str);
System.out.println(str);
}
void strToNull(String arg) {
arg = arg + "test";
}
// ミュータブル
void exec2() {
StringBuilder str = new StringBuilder("string!!");
this.strToNull2(str);
System.out.println(str.toString());
}
void strToNull2(StringBuilder arg) {
arg.append("test");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment