new StringBuilder().append("string1").append("string2").toString()このプログラムは、次の 2 つの文字列を連結します。
public class Concatenation_string {Runtime:
public static void main(String[] args) {
String s1 = "string1";
文字列 s2 = "文字列2";
文字列 s1s2 = concatenation_strings_java(s1, s2);
System.out.println(s1s2);
}
public static String concatenation_strings_java (String s1, String s2) {
return s1+s2;
}
}
string1chain2
public String concat(String s)Example:
public class Concatenation_string {Runtime:
public static void main(String[] args) {
String s = "私は一人です";
s = s.concat(" プログラマ");
System.out.println(秒);
}
}
私はプログラマーです
a+=bまず、セマンティクスに小さな違いがあります。a が null、次に a.concat(b) は NullPointerException ですが、'+'宅配便 新品", "宅配便"、ミニバン;">a+=b は a nullです。さらに、concat() は String 値のみを受け入れ、'+' 演算子は toString() ) です。メソッド concat() は、
a.concat(b)
public static void main(String[] args) {実行時にこれが必要です:
String s = null;
s = s.concat(" プログラマ");
System.out.println(秒);
}
スレッド "main" java.lang.NullPointerException'+':
at Concatenation_string.main(Concatenation_string.java:5)
public static void main(String[] args) {次の結果が得られるはずです:
String s = null;
s = s + プログラマ;
System.out.println(秒);
}
nullprogrammerしたがって、先に述べたように、演算子 '+' は以下と同等です:
a = new StringBuilder()
.append(a)
.append(b)
.toString();
String result = new StringBuilder(15).append(name).append(" ").append(firstname).toString();StringBuilder を使用して複数の文字列を連結するのがいかに簡単かがわかります。ただし、StringBuilder は、最終的な文字列の文字数と同じ容量で初期化することを忘れないでください。これにより、String を適切に初期化することで、使用できないメモリが失われるのを防ぎ、CPU 時間を短縮できます。
public static void main(String[] args) {Output:
String court ="Court of";
文字列 module_math ="数学";
/* StringBuilder */
String module_res = "networks";
int length = court.length()+module_math.length();
StringBuilder sb = new StringBuilder(length);
sb.append(court).append(module_math);
文字列 res = sb.toString();
System.out.println(res);
/* StringBuilder */
length = court.length()+module_math.length();
StringBuffer sBuffer = new StringBuffer(length);
sb.append(" and").append(court).append(module_res);
res = sb.toString();
System.out.println(res);
}
Math Courtyard
Math Courtyard and Network Courtyard
public static void main(String[] args) {10000 個の連結操作の結果:
int nb_op=10000;
文字列 s="";
double t1,t2,time;
t1= System.currentTimeMillis();
for(int i = 0 ; i < nb_op; i++)
s += "*";
t2 = System.currentTimeMillis();
時間= t2-t1;
System.out.println("+: "+time);
s="";
t1= System.currentTimeMillis();
for(int i = 0 ; i < nb_op; i++)
s.concat("*");
t2 = System.currentTimeMillis();
時間= t2-t1;
System.out.println("連結: "+時間);
StringBuilder sb=new StringBuilder();
t1= System.currentTimeMillis();
for(int i = 0 ; i < nb_op; i++)
sb.append("*");
t2 = System.currentTimeMillis();
時間= t2-t1;
System.out.println("StringBuilder: "+time);
StringBuffer sBuffer=new StringBuffer();
t1= System.currentTimeMillis();
for(int i = 0 ; i < nb_op; i++)
sBuffer.append("*");
t2 = System.currentTimeMillis();
時間= t2-t1;
System.out.println("StringBuffer: "+time);
}
+: 180.0
concat: 13.0
StringBuilder: 2.0
StringBuffer: 4.0
![]() |
Please disable your ad blocker and refresh the window to use this website.