StringBuffer类
- 作用:实现字符串内容的修改
- 构造方法:public StringBuffer()
- 构造方法:public StringBuffer(String str),可以接受字符串初始化内容
- 数据追加:public StringBuffer append(数据类型 变量),相当于字符串中的“+”操作
public class Demo {
public static void main(String[] args) {
StringBuffer buf = new StringBuffer("Hello ");
change(buf);
System.out.println(buf.toString());
}
public static void change(StringBuffer temp) {
temp.append("World");
}
}
- StringBuffer类中的其他方法
1.插入数据:public StringBuffer insert(int offset,数据类型 b)
2.删除指定范围的数据:public StringBuffer delete(int start,int end)
3.字符串内容反转:public StringBuffer reserve()
范例1:插入数据
public class Demo {
public static void main(String[] args) {
StringBuffer buf = new StringBuffer();
buf.append(".cn").insert(0,"www.").insert(4,"mldn");
System.out.println(buf);
}
}
范例2:删除指定范围的数据
public class Demo {
public static void main(String[] args) {
StringBuffer buf = new StringBuffer();
buf.append("Helloworld").delete(5,10);
System.out.println(buf);
}
}
范例3:字符串内容反转
public class Demo {
public static void main(String[] args) {
StringBuffer buf = new StringBuffer();
buf.append(".cn").insert(0,"www.").insert(4,"mldn");
System.out.println(buf);
}
}
面试题:请解释String,StringBuffer,StringBulider的区别
1.String类是字符串的首选类型,其最大的特点是内容不允许修改
2.StringBuffer和StringBulider的内容允许修改
3.StringBuffer属于线程安全的操作,而StringBulider的属于非线程安全的操作
CharSequence接口
public class Demo {
public static void main(String[] args) {
CharSequence str="www.mldn.cn"; //子类实例向父接口转型
}
}
- CharSequence中定义的操作方法’
1.获取指定索引字符:public char charAt(int index)
2.获取字符串长度:public int length()
3.截取部分字符串:public CharSenquence subSequence(int start,int end)
范例:
public class Demo {
public static void main(String[] args) {
CharSequence str = "www.mldn.cn"; //子类实例向父接口转型
CharSequence sub = str.subSequence(4, 8);
System.out.println(sub);
}
}
AutoCloseable接口
- 功能:实现资源的自动关闭(即释放资源)
- 关闭方法:public void close() throws Exception;
范例:实现自动关闭
public class JavaAPIDemo {
public static void main(String[] args) throws Exception{
try (IMessage nm = new NetMessage("www.mldn.cn")){
nm.send(); //消息发送
}catch (Exception e){}
}
}
interface IMessage extends AutoCloseable{
void send(); //消息发送
}
class NetMessage implements IMessage { //实现消息的处理机制
private String msg;
public NetMessage(String msg) {
this.msg = msg;
}
@Override
public void send() {
if (this.open()) {
System.out.println("【*** 发送消息 ***】" + this.msg);
}
}
public boolean open() { //获取资源连接
System.out.println("【OPEN】获取消息发送连接资源");
return true;
}
@Override
public void close() throws Exception{
System.out.println("【CLOSE】关闭消息发送通道");
}
}
Runtime类
- Runtime类描述的是运行时的状态
- Runtime类属于单例设计模式,如果要想获取对象,那么可以依靠类中的getRuntime()方法完成
- 获取实例化对象:public static Runtime getRuntime()
范例:获取对象实例化
public class Demo {
public static void main(String args[]) throws Exception{
Runtime run =Runtime.getRuntime();//获取对象实例化
}
}
- Runtime类中4个重要的操作方法
1.获取最大可用内存空间:public long maxMemory()//默认配置为本机系统内存的1/4
2.获取可用内存空间:public long totalMemory()//默认配置为本机系统内存的1/64
3.获取空闲内存空间:public long freeMemory()
4.手工进行GC处理:public void gc()
范例:观察内存状态
public class Demo {
public static void main(String args[]) throws Exception{
Runtime run =Runtime.getRuntime();//获取对象实例化
System.out.println(run.maxMemory()); //2113929216
System.out.println(run.totalMemory());//132120576
System.out.println(run.freeMemory());//130023424
}
}
面试题:什么时GC?如何处理?
GC指的是垃圾收集器,是可以由系统自动调用的垃圾释放功能,或者使用Runtime类中的gc()手工调用
System类
- System类中的方法
1.数组拷贝:public static void arraycopy()
2.获取当前的日期时间数值:public static long currentTimeMillis()
3.进行垃圾回收:public static void gc()
范例:操作耗时的统计
public class Demo {
public static void main(String args[]) throws Exception {
long start = System.currentTimeMillis();
Runtime run = Runtime.getRuntime();//获取对象实例化
String str = "";
for (int i = 0; i < 300; i++) {
str += "x"; //产生大量的垃圾空间
}
long end = System.currentTimeMillis();
System.out.println(end - start);
}
}
Cleaner类
- Cleaner类是一个对象清理的操作,其主要功能是进行finalize()方法的替代
范例:
class Member implements Runnable{
public Member() {
System.out.println("【构造】在一个雷电交加的日子里面,林强诞生了。");
}
@Override
public void run() {//执行清除的时候执行的是此操作
System.out.println("【回收】最终你一定要死的");
}
}
class MemberCleaning implements AutoCloseable{ //实现清除的处理
private static final Cleaner cleaner=Cleaner.create(); //创建一个清除处理
private Member member;
private Cleaner.Cleanable cleanable;
public MemberCleaning() {
this.member = new Member();//创建新对象
this.cleanable=this.cleaner.register(this,this.member); //注册使用的对象
}
@Override
public void close() throws Exception {
this.cleanable.clean(); //启动多线程
}
}
public class Demo {
public static void main(String[] args) throws Exception{
try(MemberCleaning mc=new MemberCleaning()) {
//中间可以执行一些相关的代码
}catch (Exception e){}
}
}
对象克隆
- 所谓对象克隆,就是对象的复制,而且属于全新的复制。使用克隆需要使用Object类中提供的clone()方法:protect Object clone() throws CloneNotSupportedException
范例:实现对象克隆
class Member implements Cloneable {
private String name;
private int age;
public Member(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "【" + super.toString() + "】name = " + this.name + "、age = " + this.age;
}
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone(); //调用父类提供的clone()方法
}
}
public class Demo {
public static void main(String[] args) throws Exception {
Member memberA=new Member("林强",30);
Member memberB=(Member)memberA.clone();
System.out.println(memberA);
System.out.println(memberB);
}
}