-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathConcatTest.java
More file actions
42 lines (36 loc) · 1.22 KB
/
ConcatTest.java
File metadata and controls
42 lines (36 loc) · 1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package com.Strings;
public class ConcatTest {
public static String concatWithString() {
String t = "Java";
for (int i = 0; i < 10000; i++) {
t = t + "Tpoint";
}
return t;
}
public static String concatWithStringBuffer() {
StringBuffer sb = new StringBuffer("Java");
for (int i = 0; i < 1000000; i++) {
sb.append("Tpoint");
}
return sb.toString(); //important to convet to string!!
}
public static String concatWithStringBuilder() {
StringBuilder sb = new StringBuilder("Java");
for (int i = 0; i < 1000000; i++) {
sb.append("Tpoint");
}
return sb.toString(); //important to convet to string!!
}
public static void main(String[] args) {
long startTime = System.currentTimeMillis();
concatWithString();
System.out.println("Time taken by Concating with String: " + (System.currentTimeMillis() - startTime) + "ms");
startTime = System.currentTimeMillis();
concatWithStringBuffer();
System.out.println(
"Time taken by Concating with StringBuffer: " + (System.currentTimeMillis() - startTime) + "ms");
startTime=System.currentTimeMillis();
concatWithStringBuilder();
System.out.println("Time taken by Concating with StringBuilder: "+(System.currentTimeMillis()-startTime)+"ms");
}
}