您当前的位置: 首页 >  Java

彭世瑜

暂无认证

  • 2浏览

    0关注

    2791博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

Java学习路线-17:Java基础类库StringBuffer、AutoCloseable、Runtime、System

彭世瑜 发布时间:2019-11-18 23:37:31 ,浏览量:2

第7 章 : Java基础类库 26 StringBuffer类

String有两个常量池: 静态常量池,运行时常量池

String对象实例化直接赋值形式,可以保存到常量池中以便重用

// 构造方法
public StringBuffer(String str)

// 追加
public synchronized StringBuffer append(String str)

// 插入
public synchronized StringBuffer insert(int offset, String str)

// 删除指定范围数据
public synchronized StringBuffer delete(int start, int end)

// 字符串内容反转
public synchronized StringBuffer reverse()

代码示例


class Demo{
    public static void main(String[] args) {
        StringBuffer sb = new StringBuffer("hello");
        sb.append(" world");
        System.out.println(sb.toString());
        // hello world
    }
}

String 转换为 StringBuffer 使用构造方法 StringBuffer 转换为 String 使用toString

操作示例

class Demo{
    public static void main(String[] args) {
        StringBuffer sb = new StringBuffer("hello");

        // 可以连续操作
        sb.append(" ").append("world");
        System.out.println(sb.toString());
        // hello world
        
        // 插入
        sb.insert(5, " Java");
        System.out.println(sb);
        // hello Java world

        // 删除
        sb.delete(5, 10);
        System.out.println(sb);
        // hello world
        
        // 反转
        sb.reverse();
        System.out.println(sb);
        // dlrow olleh

    }
}

类似功能类 StringBuilder JDK >= 1.5

区别 String 字符串,内容不可修改 StringBuffer JDK>=1.0 线程安全, 使用了synchronized StringBuilder JDK>=1.5 非线程安全

27 CharSequence接口

CharSequence描述字符串结构的接口


public final class String
    implements java.io.Serializable, Comparable, CharSequence

public final class StringBuffer
    extends AbstractStringBuilder
    implements java.io.Serializable, CharSequence

public final class StringBuilder
    extends AbstractStringBuilder
    implements java.io.Serializable, CharSequence

CharSequence接口方法


public interface CharSequence{
    int length();
    char charAt(int index);
    CharSequence subSequence(int start, int end);
    public String toString();
}

28 AutoCloseable接口

AutoCloseable接口 用于资源的自动关闭 JDK >= 1.7

public interface AutoCloseable {
    void close() throws Exception;
}

不使用自动关闭代码示例


interface IMessage{
    public void send();
}

class MessageImpl implements IMessage{
    private String message;

    public MessageImpl(String message) {
        this.message = message;
    }

    @Override
    public void send() {
        System.out.println("发送消息: " + this.message);
    }

    public boolean open(){
        System.out.println("打开资源");
        return true;
    }

    public void close(){
        System.out.println("关闭资源");
    }
}

class Demo{
    public static void main(String[] args) {
        MessageImpl message = new MessageImpl("消息内容");
        if(message.open()){
            message.send();
            message.close();
        }
        /**
         * 打开资源
         * 发送消息: 消息内容
         * 关闭资源
         */

    }
}

结合异常处理语句实现资源自动关闭

interface IMessage extends AutoCloseable{
    public void send();
}

class MessageImpl implements IMessage{
    private String message;

    public MessageImpl(String message) {
        this.message = message;
    }

    @Override
    public void send() {
        System.out.println("发送消息: " + this.message);
    }

    public boolean open(){
        System.out.println("打开资源");
        return true;
    }

    public void close(){
        System.out.println("关闭资源");
    }
}

class Demo{
    public static void main(String[] args) {
        try(MessageImpl message = new MessageImpl("消息内容")){
            if(message.open()){
                message.send();
            }
        }catch(Exception e){

        }

        /**
         * 打开资源
         * 发送消息: 消息内容
         * 关闭资源
         */

    }
}
29 Runtime类

Runtime描述运行时状态 一个JVM进程只允许提供一个Runtime,使用了单例设计模式 可以使用静态方法获取实例化对象

public static Runtime getRuntime()

class Demo {
    public static void main(String[] args) {
        Runtime run = Runtime.getRuntime();
        
        // 读取CPU内核数量
        System.out.println(run.availableProcessors());
        //  8

        // 获取最大可用内存空间 1/4
        System.out.println(run.maxMemory());

        // 获取可用内存空间 1/64
        System.out.println(run.totalMemory());

        // 获取空闲内存空间
        System.out.println(run.freeMemory());
        
        // 手工进行GC处理
        run.gc();

    }
}

GC (Garbage Collector) 垃圾收集器 由系统自动调用 或者使用Runtime类中的gc()方法,手工清除

30 System类

常用方法

// 数组拷贝
public static native void arraycopy(Object src,  int  srcPos,
                                    Object dest, int destPos,
                                    int length);

// 获取时间数值
System.out.println(System.currentTimeMillis());
// 1573918888172

// 垃圾回收
public static void gc() {
    Runtime.getRuntime().gc();
}

31 Cleaner类

JDK>=1.9 Cleaner类提供对象清理操作 替代finialize()方法 C++提供了构造函数,析构函数 Java垃圾使用GC回收


class Demo {
    public Demo() {
        System.out.println("构造函数");
    }

    @Override
    protected void finalize() throws Throwable {
        System.out.println("垃圾回收");
        super.finalize();
    }

    public static void main(String[] args) {
        Demo demo = new Demo();
        demo = null;
        System.gc();
        /**
         * 构造函数
         * 垃圾回收
         */
    }
}

不建议使用 finalize()方法, 使用AutoCloseable接口 使用Cleaner类,使用单独的线程去回收资源,能提高整体性能

32 对象克隆
protected native Object clone() throws CloneNotSupportedException;

// 只有接口名,没有任何方法,只是能力标识接口
public interface Cloneable {
}

接口作用: 标准 能力


class Demo implements Cloneable{
    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }

    public static void main(String[] args) {
        Demo demo = new Demo();
        Demo demo2 = null;

        try {
            demo2 = (Demo)demo.clone();
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }

        System.out.println(demo);
        System.out.println(demo2);
        /**
         * Demo@2503dbd3
         * Demo@4b67cf4d
         */
    }
}
关注
打赏
1665367115
查看更多评论
立即登录/注册

微信扫码登录

0.2646s