Java类库之StringBuffer类(重点)

在讲解StringBuffer类之前首先来简单回顾一下String类的特点:

· String类的对象有两种实例化方式,一种是直接赋值,只会开辟一块堆内存空间,而且对象可以自动入池,另外一种方式使用构造方法完成,会开辟两块空间,有一块空间将成为垃圾,并且不会自动入池,但是可以通过intern()方法手工入池;
· 字符串常量一旦声明则不可改变,而字符串对象可以改变,但是改变的是其内存地址的指向;

通过以上的几个特点就可以清楚的发现,String类是表示字符串使用最多的类,但是其不适合于被频繁修改的字符串操作上,所以在这种情况下,往往可以使用StringBuffer类,即:StringBuffer类方便用户进行内容的修改。在String类之中使用“+”作为数据库的连接操作,而在StringBuffer类之中使用append()方法进行数据的连接。

范例:使用StringBuffer操作,StringBuffer的内容可以改变

   public class TestDemo {
    	public static void main(String[] args) throws Exception {
    		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 PKU");
    	}
    }

StringBuffer类在日后主要用于频繁修改字符串的操作上,但是在任何的开发之中,面对字符串的操作,98%都先考虑String,只有那2%会考虑StringBuffer。
现在表示字符串的操作类就有了两个:String、StringBuffer,那么下面通过这两个类的定义来研究一下关系:
现在发现String和StringBuffer类都实现了一个CharSequence接口,日后一定要记住,如果看见了CharSequence最简单的理解做法就是传字符串,但是虽然这两个类是同一个接口的子类,不过这两个类对象之间却不能互相直接转型。

操作一:将String变为StringBuffer
· 方法一:直接利用StringBuffer类的构造方法,public StringBuffer(String str)

public class TestDemo {
	public static void main(String[] args) throws Exception {
		String str = "Hello World." ;
		StringBuffer buf = new StringBuffer(str);
		System.out.println(buf);
	}
}

· 方法二:利用StringBuffer类的append()方法

public class TestDemo {
	public static void main(String[] args) throws Exception {
		String str = "Hello World ." ;
		StringBuffer buf = new StringBuffer();
		buf.append(str) ;
		System.out.println(buf);	
   }
}

操作二:将StringBuffer变为String,利用StringBuffer类的toString()方法完成

public class TestDemo {
	public static void main(String[] args) throws Exception {
		StringBuffer buf = new StringBuffer();
		buf.append("Hello World .") ;
		String str = buf.toString() ;
		System.out.println(str); 
	}
}

在String类之中定义了许多的操作方法,同样,在StringBuffer类之中也定义了许多的操作方法,而且有些方法还是String类所有没有的支持。

范例:字符串反转操作,public StringBuffer reverse()

public class TestDemo {
	public static void main(String[] args) throws Exception {
		StringBuffer buf = new StringBuffer();
		buf.append("Hello World .") ;
		System.out.println(buf.reverse()); 
	}
}

范例:替换指定范围内的数据,public StringBuffer replace(int start, int end, String str)

public class TestDemo {
	public static void main(String[] args) throws Exception {
		StringBuffer buf = new StringBuffer();
		buf.append("Hello World .") ;
		System.out.println(buf.replace(6, 12, "PKU")); 
	}
}

范例:在指定位置上插入数据,public StringBuffer insert(int offset, 数据类型 变量)

public class TestDemo {
	public static void main(String[] args) throws Exception {
		StringBuffer buf = new StringBuffer();
		buf.append("World .").insert(0, "Hello ") ;
		System.out.println(buf); 
	}
}

面试题:请解释String和StringBuffer的区别?

String的内容不可改变,而StringBuffer的内容可以改变。

全部评论

相关推荐

这是什么操作什么意思,这公司我服了...
斯派克spark:意思是有比你更便宜的牛马了
点赞 评论 收藏
分享
06-07 19:59
门头沟学院 C++
补药卡我啊😭:都快15年前的了还在11新特性
你的简历改到第几版了
点赞 评论 收藏
分享
一表renzha:手写数字识别就是一个作业而已
点赞 评论 收藏
分享
就前几天旅游的时候,打开抖音就经常刷到这类视频:以前是高学历学生、老师、主持人,现在做着团播、擦边主播的工作,以及那些经过精心包装的“职业转型”故事——从铺天盖地的VLOG到所谓的“04年夜场工作日记”,这些内容在初中升学、高考放榜等关键时间节点持续发酵。可以说非常直接且精准地在潜移默化地影响着心智尚未成熟的青少年,使其对特殊行业逐渐脱敏。那我就想问了:某些传播公司、平台运营者甚至某些夜场的老板,你们究竟在传递怎样的价值观?点开那些视频,评论区里也是呈现明显的两极分化:一种是​​经济下行论​​:“现在就业市场已经艰难到这种程度了吗?”​​一种是事实反驳派​​:这些创作者往往拥有名校背景,从事着...
牛客刘北:被环境教育的,为了能拿到足够的钱养活自己,不甘心也得甘心,现在的短视频传播的思想的确很扭曲,但是很明显,互联网玩上一年你就能全款提A6,但你全心全意不吃不喝工作一年未必能提A6,但是在高考中考出现这个的确很扭曲,在向大家传播“不上学,玩互联网也可以轻松年入百万”,不是人变了,是社会在变
预测一下26届秋招形势
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务