Last active
November 16, 2017 12:36
-
-
Save snicmakino/d51322220c79ed82e7d76c1fe71936a6 to your computer and use it in GitHub Desktop.
Stringがイミュータブルである事の簡易的な説明用
This file contains 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
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