时间:2021-05-02
前言
在java开发者中,字符串的拼接占用资源高往往是热议的话题.
让我们深入讨论一下为什么会占用高资源。
在java中,字符串对象是不可变的,意思是它一旦创建,你就无法再改变它。所以在我们拼接字符串的时候,创建了一个新的字符串,旧的被垃圾回收器所标记。
如果我们处理上百万的字符串,然后,我们就会生成百万的额外字符串被垃圾回收器处理。
在大多数的教程中,也许你会看到用+号拼接字符串会生成多个string,导致性能过差,建议使用stringbuffer/stringbuilder来拼接。
可是真的是这样的吗?
本文在jdk8中做了如下实验:
? 1 2 3 4 5 public static void main(string[] args) { string result = ""; result += "some more data"; system.out.println(result); }通过javap -c来反编译得到:
? 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 code: 0: aload_0 // push 'this' on to the stack 1: invokespecial #1 // invoke object class constructor // pop 'this' ref from the stack 4: return // return from constructor public static void main(java.lang.string[]); code: 0: ldc #2 // load constant #2 on to the stack 2: astore_1 // create local var from stack (pop #2) 3: new #3 // push new stringbuilder ref on stack 6: dup // duplicate value on top of the stack 7: invokespecial #4 // invoke stringbuilder constructor // pop object reference 10: aload_1 // push local variable containing #2 11: invokevirtual #5 // invoke method stringbuilder.append() // pop obj reference + parameter // push result (stringbuilder ref) 14: ldc #6 // push "some more data" on the stack 16: invokevirtual #5 // invoke stringbuilder.append // pop twice, push result 19: invokevirtual #7 // invoke stringbuilder.tostring:(); 22: astore_1 // create local var from stack (pop #6) 23: getstatic #8 // push value system.out:printstream 26: aload_1 // push local variable containing #6 27: invokevirtual #9 // invoke method printstream.println() // pop twice (object ref + parameter) 30: return // return void from method可以看到java编译器优化了生成的字节码,自动创建了一个stringbuilder,并进行append操作。
由于构建最终字符串的子字符串在编译时已经已知了,在这种情况下java编译器才会进行如上的优化。这种优化称为a static string concatenation optimization,自jdk5时就开始启用。
那是否就能说明在jdk5以后,我们不再需要手动生成stringbuilder,通过+号也能达到同样的性能?
我们尝试下动态拼接字符串:
动态拼接字符串指的是仅在运行时才知道最终字符串的子字符串。比如在循环中增加字符串:
? 1 2 3 4 5 6 7 public static void main(string[] args) { string result = ""; for (int i = 0; i < 10; i++) { result += "some more data"; } system.out.println(result); }同样反编译:
? 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 code: 0: aload_0 // push 'this' on to the stack 1: invokespecial #1 // invoke object class constructor // pop 'this' ref from the stack 4: return // return from constructor public static void main(java.lang.string[]); code: 0: ldc #2 // load constant #2 on to the stack 2: astore_1 // create local var from stack, pop #2 3: iconst_0 // push value 0 onto the stack 4: istore_2 // pop value and store it in local var 5: iload_2 // push local var 2 on to the stack 6: i2d // convert int to double on // top of stack (pop + push) 7: ldc2_w #3 // push constant 10e6 on to the stack 10: dcmpg // compare two doubles on top of stack // pop twice, push result: -1, 0 or 1 11: ifge 40 // if value on top of stack is greater // than or equal to 0 (pop once) // branch to instruction at code 40 14: new #5 // push new stringbuilder ref on stack 17: dup // duplicate value on top of the stack 18: invokespecial #6 // invoke stringbuilder constructor // pop object reference 21: aload_1 // push local var 1 (empty string) // on to the stack 22: invokevirtual #7 // invoke stringbuilder.append // pop obj ref + param, push result 25: ldc #8 // push "some more data" on the stack 27: invokevirtual #7 // invoke stringbuilder.append // pop obj ref + param, push result 30: invokevirtual #9 // invoke stringbuilder.tostring // pop object reference 33: astore_1 // create local var from stack (pop) 34: iinc 2, 1 // increment local variable 2 by 1 37: goto 5 // move to instruction at code 5 40: getstatic #10 // push value system.out:printstream 43: aload_1 // push local var 1 (result string) 44: invokevirtual #11 // invoke method printstream.println() // pop twice (object ref + parameter) 47: return // return void from method可以看到在14的时候new了stringbuilder,但是在37的时候goto到了5,在循环过程中,并没有达到最优化,不断在生成新的stringbuilder。
所以上述代码类似:
? 1 2 3 4 5 6 7 8 string result = ""; for (int i = 0; i < 10; i++) { stringbuilder tmp = new stringbuilder(); tmp.append(result); tmp.append("some more data"); result = tmp.tostring(); } system.out.println(result);可以看到不断生成新的stringbuilder,并且通过tostring,原来的stringbuilder将不再引用,作为垃圾,也增加了gc成本。
所以,在实际的使用中,当你无法区分字符串是静态拼接还是动态拼接的时候,还是使用stringbuilder吧。
reference:
http://www.pellegrino.link/2015/08/22/string-concatenation-with-java-8.html
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对服务器之家的支持。
原文链接:https://my.oschina.net/hosee/blog/1786130
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
java8、jdk8日期转化成字符串新建日期工具类:DateUtils新建方法:parseDate实现方法parseDatepublicstaticString
本文介绍了vue语法之拼接字符串的示例代码,分享给大家,具体如下。先来一行代码: 如代码所示,只需要在数组语法中拼接字符串即可。***知识点***顺便总结一下
是什么字符串常量池是JVM中的一个重要结构,用于存储JVM运行时产生的字符串。在JDK7之前在方法区中,存储的是字符串常量。而字符串常量池在JDK7开始移入堆中
1,拼接字符串拼接字符串是最常用到的字符串操作之一,在PHP中支持三种方式对字符串进行拼接操作,分别是圆点.分隔符{}操作,还有圆点等号.=来进行操作,圆点等号
#字符串拼接concat(s1,s2);将表中last_name和first_name中的字符串拼接selectconcat(last_name,first_n