String
・文字列の操作は、StringBuilderを利用することでパフォーマンスが向上
→特にループ内で+での文字列結合を行わないこと
bad.java
static String concat(String[] array) {
String result = "";
for (String s : array) {
result += s; //
}
return result;
}
↓
better.java
static String concat(String[] array) {
StringBuiler result = new StringBuiler();
for (String s : array) {
result.append(s);
}
return result.toString();
}
※StringBuiler 比較は、注意が必要
StringBuiler_equals.java
StringBuilder sb1 = new StringBuilder("ABC");
StringBuilder sb2 = new StringBuilder("ABC");
if(sb1 == sb2){
System.out.println("sb1 == sb2" + " is OK.");
}else{
System.out.println("sb1 == sb2" + " is NG.");//FALSE
}
if(sb1.equals(sb2)){
System.out.println("sb1 equals sb2" + " is OK.");
}else{
System.out.println("sb1 equals sb2" + " is NG.");//FALSE
}
if(sb1.toString().contentEquals(sb2)){
System.out.println("sb1 contentEquals sb2" + " is OK.");//TRUE
}else{
System.out.println("sb1 contentEquals sb2" + " is NG.");
}