new StringBuilder().append("string1").append("string2").toString()This program concatenates two strings:
public class Concatenation_string {Runtime:
public static void main(String[] args) {
String s1 = "string1";
String s2 = "string2";
String 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) {< br /> String s = "I am one";
s = s.concat(" programmer");
System.out.println(s);
}
}
I'm a programmer
a+=bFirst, there's a small difference in the semantics. If a is null, then a.concat(b) triggers a NullPointerException, but the '+' in the statement a+=b processes the value of a even if it is null. In addition, the concat() accepts only String values while the '+' operator converts the value to a String( using the toString() ). We can say that the method concat() is stricter in that it accepts.
a.concat(b)
public static void main(String[] args) {You should have this at runtime:
String s = null;
s = s.concat(" programmer");
System.out.println(s);
}
Exception in thread "main" java.lang.NullPointerExceptionThe same example with the '+':
at Concatenation_string.main(Concatenation_string.java:5)
public static void main(String[] args) {You should have this result:
String s = null;
s = s + programmer;
System.out.println(s);
}
nullprogrammerSo, as we said earlier, the operator '+' is equivalent to:
a = new StringBuilder()
.append(a)
.append(b)
.toString();
String result = new StringBuilder(15).append(name).append(" ").append(firstname).toString();You can see how easy it is to concatenate multiple Strings using StringBuilder. However, remember to initialize StringBuilder with a capacity equal to the number of characters in the final string. This will prevent losing unusable memory by properly initializing our String and reduce CPU time.
public static void main(String[] args) {Output:
String court ="Court of";
String module_math ="math";
/* StringBuilder */
String module_res = "networks";
int length = court.length()+module_math.length();
StringBuilder sb = new StringBuilder(length);
sb.append(court).append(module_math);
String 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) {Results for 10000 concatenation operations:
int nb_op=10000;
String s="";
double t1,t2,time;
t1= System.currentTimeMillis();
for(int i = 0 ; i < nb_op; i++)
s += "*";
t2 = System.currentTimeMillis();
time = t2-t1;
System.out.println("+: "+time);
s="";
t1= System.currentTimeMillis();
for(int i = 0 ; i < nb_op; i++)
s.concat("*");
t2 = System.currentTimeMillis();
time = t2-t1;
System.out.println("concat: "+time);
StringBuilder sb=new StringBuilder();
t1= System.currentTimeMillis();
for(int i = 0 ; i < nb_op; i++)
sb.append("*");
t2 = System.currentTimeMillis();
time = 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();
time = t2-t1;
System.out.println("StringBuffer: "+time);
}
+: 180.0
concat: 13.0
StringBuilder: 2.0
StringBuffer: 4.0
![]() |
Time in milliseconds for 10000 operations |
Please disable your ad blocker and refresh the window to use this website.