String 中 "+"的实现

***String 中 "+"的实现

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
by Object and inherited by all classes in Java. For additional information on string concatenation and conversion,
see Gosling, Joy, and Steele, The Java Language Specification.
        解释一下它这个api,String类型的对象使用“+”进行串连接的时候,是通过StringBuffer或者是StringBuilder的append()方法来实现的,这里可以做一个大胆的猜想:也就是说在执行“+”操作时String对象会将自身构造成StringBuffer或者StringBuilder对象,然后再通过他们的append()方法来实现,之后调用StringBuffer或者StringBuilder的toString()方法返回给表达式,在看看StringBuffer或者StringBuilder的toString()方法的源码:


参考文章:

1.关于java的String的探讨  http://www.2cto.com/kf/201206/136505.html

2.JAVA中的String连接性能  http://www.blogjava.net/javagrass/archive/2010/01/24/310650.html

3.JDK 1.5 String API  http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/String.html


你可能感兴趣的:(String 中 "+"的实现)