close

http://download.oracle.com/javase/6/docs/api/java/lang/String.html
節錄如下:
The Java language provides special support for the string concatenation operator ( + ), and for conversion of other objects to strings. String concatenation is implemented through the StringBuilder(or StringBuffer) class and its append method. String conversions are implemented through the method toString, defined byObject and inherited by all classes in Java. For additional information on string concatenation and conversion, see Gosling, Joy, and Steele, The Java Language Specification.

 

String s = x + y + z;

等同於: String s = new StringBuilder(x).append(y).append(z);


但 String s = x;
s += y;
s += z;

這種寫法代表要StringBuilder三次  然後toString()三次!

較浪費系統資源!!!

 

學到了!!!可以使用~append方法替代

StringBuilder sb= new StringBuilder(x);

sb.append(y).append(z);

arrow
arrow
    全站熱搜

    stacychung 發表在 痞客邦 留言(0) 人氣()