StringBuilder vs String concatenation in toString() in Java
2011-03-05 11:20
639 查看
http://stackoverflow.com/questions/1532461/stringbuilder-vs-string-concatenation-in-tostring-in-java
Question
===========================================================
Given the 2
or
More importantly given we have only 3 properties it might not make a difference, but at what point do you switch from concat to builder?
Answer:
===========================================================
Version 1 is preferable because it is shorter and the compiler will in fact turn it into version 2 - no performance difference whatsoever.
More importantly given we have only 3 properties it might not make a difference, but at what point do you switch from concat to builder?
At the point where you're concatenating in a loop - that's usually when the compiler can't substitute StringBuilder by itself.
Question
===========================================================
Given the 2
toString()implementations below, which is prefered
public String toString(){ return "{a:"+ a + ", b:" + b + ", c: " + c +"}"; }
or
public String toString(){ StringBuilder sb = new StringBuilder(100); return sb.append("{a:").append(a) .append(", b:").append(b) .append(", c:").append(c) .append("}") .toString(); }
More importantly given we have only 3 properties it might not make a difference, but at what point do you switch from concat to builder?
Answer:
===========================================================
Version 1 is preferable because it is shorter and the compiler will in fact turn it into version 2 - no performance difference whatsoever.
More importantly given we have only 3 properties it might not make a difference, but at what point do you switch from concat to builder?
At the point where you're concatenating in a loop - that's usually when the compiler can't substitute StringBuilder by itself.
相关文章推荐
- Java String vs StringBuilder vs StringBuffer Concatenation Performance Micro Benchmark
- "String Concatenation vs String Builder - The performance hit! See it to believe it :o)"
- String vs StringBuffer vs StringBuilder in Java
- Java - String vs StringBuffer vs StringBuilder in Java
- java-ToStringBuilder介绍
- Java String Concatenation and Performance(String串联4个操作性能对比)
- How to encode decode String in Java base64 Encoding
- StringBuffer使用append提示String concatenation as argument to 'StringBuffer.append()' call
- How to convert from string to datetime in sql server? - Sealyu - BlogJava
- Java 工具类 ToStringBuilder
- Unable to find a value for "dwmc" in object of class "java.lang.String" using operator "." (null
- java-ToStringBuilder介绍
- [Java Tips] How to Use StringTokenizer in Java?
- Exception in thread "main" java.lang.ClassCastException: java.lang.String cannot be cast to java.lan
- Use of uninitialized value in concatenation (.) or string at test.pl line 232
- output buffering vs string concatenation / ob vs 拼接字符串
- Convert a List to a Comma-Separated String in Java 8
- java 打印对象的属性和值ToStringBuilder.reflectionToString
- Unable to find a value for "字段名" in object of class java.lang.String using operator "."
- Efficient String Concatenation in Python