字符连接,StringBuffer类

概念:StringBuffer本身也属于一个字符串的操作类,有两个意义:一个意义是可以快速的修改,另外一个意义是可以快速读取。String类对象的内容不可改变,而StringBuffer类对象的内容可以改变。

使用方法:StringBuffer需要使用new明确调用构造方法后,使用append()进行连接。 

                   · append():public StringBuffer append(数据类型 s)

代码实现:

 1 package cn.demo;  
 2 public class Test {  
 3     public static void main(String[] args) throws Exception {  
 4         StringBuffer buf = new StringBuffer();  
 5         buf.append("hello").append(" word").append("!!!");  
 6         fun(buf);  
 7         System.out.println(buf);  
 8     }  
 9     public static void fun(StringBuffer temp){  
10         temp.append("
").append("hello liyang.");  
11     }  
12 } 

结果:hello world!!!

     hello liyang.

总结:String与StringBuffer的最大区别在于String类对象不可改变,而StringBuffer类对象可以改变。

原文地址:https://www.cnblogs.com/liyang31/p/5802778.html