黑马程序员技术交流社区
标题:
连接字符串
[打印本页]
作者:
songwenhao
时间:
2016-4-25 23:27
标题:
连接字符串
以下实例演示了通过 "+" 操作符和StringBuffer.append() 方法来连接字符串,并比较其性能:
//StringConcatenate.java 文件
public
class
StringConcatenate
{
public
static
void
main
(
String
[]
args
){
long
startTime
=
System
.
currentTimeMillis
();
for
(
int
i
=
0
;
i
<
5000
;
i
++){
String
result
=
"This is"
+
"testing the"
+
"difference"
+
"between"
+
"String"
+
"and"
+
"StringBuffer"
;
}
long
endTime
=
System
.
currentTimeMillis
();
System
.
out
.
println
(
"字符串连接"
+
" - 使用 + 操作符 : "
+
(
endTime
-
startTime
)+
" ms"
);
long
startTime1
=
System
.
currentTimeMillis
();
for
(
int
i
=
0
;
i
<
5000
;
i
++){
StringBuffer
result
=
new
StringBuffer
();
result
.
append
(
"This is"
);
result
.
append
(
"testing the"
);
result
.
append
(
"difference"
);
result
.
append
(
"between"
);
result
.
append
(
"String"
);
result
.
append
(
"and"
);
result
.
append
(
"StringBuffer"
);
}
long
endTime1
=
System
.
currentTimeMillis
();
System
.
out
.
println
(
"字符串连接"
+
" - 使用 StringBuffer : "
+
(
endTime1
-
startTime1
)+
" ms"
);
}
}
以上代码实例输出结果为:
作者:
liuchenguangqnm
时间:
2016-4-26 07:09
性能比较在哪里嘞= =?
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2