//stringcompareperformance.java 文件public class stringcompareperformance{ public static void main(string[] args){ long starttime = system.currenttimemillis(); for(int i=0;i<50000;i++){ string s1 = "hello"; string s2 = "hello"; } long endtime = system.currenttimemillis(); system.out.println("通过 string 关键词创建字符串" + " : "+ (endtime - starttime) + " 毫秒" ); long starttime1 = system.currenttimemillis(); for(int i=0;i<50000;i++){ string s3 = new string("hello"); string s4 = new string("hello"); } long endtime1 = system.currenttimemillis(); system.out.println("通过 string 对象创建字符串" + " : " + (endtime1 - starttime1) + " 毫秒"); }}
以上代码实例输出结果为:
通过 string 关键词创建字符串 : 6 毫秒 通过 string 对象创建字符串 : 14 毫秒
以上就是java 实例 - 字符串性能比较测试的内容。