每天学点Java知识 ------> StringBuffer类
上一篇 /
下一篇 2014-07-23 17:08:38
/ 个人分类:Java学习
在
java中,字符串使用String类进行表示,但是String类所表示的字符串有一个最大的问题:“
字符串常量一旦声明则不可改变,而字符串对象可以改变,但是改变的是其内存地址的指向”。所以String类不适于被频繁修改的字符串操作上,在这种情况下就可以使用StringBuffer类,而StringBuffer类方便用户进行内容修改,在String类中使用"+"作为
数据库的连接操作,而在StringBuffer类中使用append()方法进行数据的连接
例如:
public class Test{
public static void main(String args[]){
StringBuffer buf = new StringBuffer();
buf.append("Hello ").append("World.");
fun(buf);
System.out.println(buf);
}
public static void fun(StringBuffer temp){
temp.append("\n").append("Hello Test");
}
}
程序运行结果:
Hello World
Hello Test
程序首先声明了一个StringBuffer类的对象,而后使用append()方法进行了字符串数据的连接,由于StringBuffer对象的内容可以改变,所以在fun()方法中对其所做的修改会被保留下来
收藏
举报
TAG: