Java—常用类库(一)StringBuffer类、Runtime类

10.1:StringBuffer类:

1.

在java中,字符串使用String表示,但是String类所表示的字符串有一个最大的问题:字符串常量一旦声明则不可改变,而字符串对象可以改变,但改变的是其内存地址的指向。所以String类不适合于被频繁修改的字符串操作中,所以在这种情况下,往往可以使用StringBuffer类,更方便于用户进行内容的修改。在String类中使用“+”作为数据库的连接操作,而在StringBuffer类中使用append()方法进行数据的连接。

2.观察StringBuffer的基本使用:
public class TestDemo {
    public static void main(String[] args) throws Exception {
        StringBuffer buf = new StringBuffer() ;
        buf.append("Hello").append("MLDN").append("!!") ;
        change(buf) ;
        System.out.println(buf) ;

    }
    public static void change(StringBuffer temp){
        temp.append("\n").append("!!!");
    }
}

本程序利用StringBuffer类对象实现了引用传递,并且发现,在change()方法中针对StringBuffer对象的修改可以影响原始StringBuffer类对象,所以StringBuffer对象的内容是可以修改的。

3.虽然String和StringBuffer类都属于CharSequence接口的子类。但这两个类不能直接进行转换。
原则一:将String转换为StringBuffer类对象。

方法一:
利用StringBuffer类的构造方法(public StringBuffer(String str))。

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

在StringBuffer类中提供了一个专门接收String类对象的构造方法,利用此构造方法可以将传递进来的String类对象实例化为StringBuffer类对象。
方法二:
利用StringBuffer类中的append()方法(public StringBuffer append(String str))。

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

本程序首先实例化了一个StringBuffer类对象,然后利用append()方法向StringBuffer类对象中增加了一个String类对象,这样就相当于将String类对象变为StringBuffer类对象。

原则二:将StringBuffer类变为String。

方法一:
利用toString()方法可以将StringBuffer转换为String。

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

实际上所有的类都会继承Object类的toString()方法,所以所以的类对象都可以转换为String对象。
方法二:
利用String类的构造方法(public String(StringBuffer buffer))实现StringBuffer与String的转换。

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

本程序利用了String类的构造方法接收了StringBuffer类对象,这样就实现了StringBuffer转换为String类对象的操作。

4.StringBuffer类常用操作方法:

1.字符串反转:

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

2.在指定的索引位置增加数据:

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

3。删除部分数据:

public class TestDemo1 {
    public static void main(String[] args) throws Exception{
        StringBuffer buf = new StringBuffer("Hi Hello World") ;
        System.out.println(buf.delete(5,11));
    }
}

10.2 Runtime类:

在每一个JVM进程中都会存在一个运行时的操作类的对象,而这个对象所属的类型就是Runtime类。利用Runtime类可以启动新的进程或进行相关运行时环境的 操作,例如:取得内存空间以及释放垃圾空间。

1.Runtime类的常用方法:

a.取得Runtime实例化对象:public static Runtime getRuntime()
b.返回最大可用内存大小:public long maxMemory()
c.返回所有可用内存大小:public long totalMemory()
d.返回所有空余内存大小:public long freeMemory()
e.执行垃圾回收操作:pulic void gc()
f.创建新的进程:public Process exec(String command)throws IOException

2.范例:

a.观察内存大小:

public class TestDemo {
    public static void main(String[] args) throws Exception {
        Runtime run = Runtime.getRuntime();
        System.out.println("MAX=" + run.maxMemory());
        System.out.println("total=" + run.totalMemory());
        System.out.println("FREE=" + run.freeMemory());
    }
}

程序运行结果:

MAX=2099249152
total=132120576
FREE=129517640

本程序动态取得当前系统中的各个内存空间信息,返回的单位是字节。
b.观察gc()使用前后的内存占用率问题:

public class TestDemo {
    public static void main(String[] args) throws Exception {
        Runtime run = Runtime.getRuntime();
        String str = "" ;
        for(int x = 0 ; x < 2000 ; x++){
            str += x ;
        }
        System.out.println("【垃圾处理前的内存量】MAX=" + run.maxMemory());
        System.out.println("【垃圾处理前的内存量】TOTAl=" + run.totalMemory());
        System.out.println("【垃圾处理前的内存量】FREE=" + run.freeMemory());
        run.gc();
        System.out.println("【垃圾处理后的内存量】MAX=" + run.maxMemory());
        System.out.println("【垃圾处理后的内存量】TOTAl=" + run.totalMemory());
        System.out.println("【垃圾处理后的内存量】FREE=" + run.freeMemory());
    }
}

程序执行结果:

【垃圾处理前的内存量】MAX=2099249152
【垃圾处理前的内存量】TOTAl=132120576
【垃圾处理前的内存量】FREE=122711920
【垃圾处理后的内存量】MAX=2099249152
【垃圾处理后的内存量】TOTAl=10485760
【垃圾处理后的内存量】FREE=9243760

本程序利用for循环产生了许多垃圾空间,通过输出可以发现,垃圾产生后的闲置空间已经明显减少,而调用了gc()方法后空间将得到释放。
c.实际上Runtime类还有一个有意思的功能,就是说它可以调用本机的可执行程序,并且创建进程。
创建“mspaint.exe”(Windows的画板)进程

public class TestDemo1 {
    public static void main(String[] args) throws Exception {
        Runtime run = Runtime.getRuntime() ;
        Process pro = run.exec("mspaint.exe");
        Thread.sleep(2000);
        pro.destroy();
    }
}

本程序运行后会立刻在系统中增加一个画板程序的进程,过2s后自动销毁。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值