Java基础类库

2020-12-16  本文已影响0人  曾梦想仗剑天涯

StringBuffer类

//String类对象引用传递
public class StringBufferFun {
    public static void main(String[] args) {
        String str = "Hello";
        change(str);
        System.out.println(str);
    }
    public static void change(String temp) {
        temp += " World";
    }
}

//StringBuffer类对象引用传递
public class StringBufferFun {
    public static void main(String[] args) {
        StringBuffer str = new StringBuffer("Hello");
        change(str);
        System.out.println(str);
    }
    public static void change(StringBuffer temp) {
        temp.append(" World!");
    }
}
//插入数据
public class StringBufferFun {
    public static void main(String[] args) {
        StringBuffer str = new StringBuffer();
        str.append(" World").insert(0, "Hello");
        System.out.println(str);
    }
}

//删除数据
public class StringBufferFun {
    public static void main(String[] args) {
        StringBuffer str = new StringBuffer("Hello World!");
        str.delete(5, 12);
        System.out.println(str);
    }
}

//字符串反转
public class StringBufferFun {
    public static void main(String[] args) {
        StringBuffer str = new StringBuffer("Hello World!");
        str.reverse();
        System.out.println(str);
    }
}

CharSequence接口

String类 StringBuffer类 StringBuilder类
public final class String extends Object implements Serializable, Comparable<String>, CharSequence public final class StringBuffer extends Object implements Serializable, CharSequence public final class StringBuilder extends Object implements Serializable, CharSequence
此图来源于李兴华老师
public class JavaFun {
    public static void main(String[] args) {
        CharSequence str = "Hello World";       //子类实例向父接口转型
    }
}

AutoCloseable接口

//模拟消息发送
package com.company;
interface IMessage {
    public void send();
}
class NetMessage implements IMessage {
    private String msg;
    public NetMessage(String msg) {
        this.msg = msg;
    }
    public boolean open() {
        System.out.println("消息链接成功");
        return true;
    }
    @Override
    public void send() {
        if (this.open()) {
            System.out.println("消息发送:" + this.msg);
        }
    }
    public void close() {
        System.out.println("消息断开链接");
    }
}
public class AutoCloseableApi {
    public static void main(String[] args) {
        NetMessage nm = new NetMessage("Hello World");
        nm.send();
        nm.close();
    }
}
此图来源于李兴华老师
package com.company;
interface IMessage extends AutoCloseable {
    public void send();
}
class NetMessage implements IMessage {
    private String msg;
    public NetMessage(String msg) {
        this.msg = msg;
    }
    public boolean open() {
        System.out.println("消息链接成功");
        return true;
    }
    @Override
    public void send() {
        if (this.open()) {
            System.out.println("消息发送:" + this.msg);
        }
    }
    public void close() {
        System.out.println("消息断开链接");
    }
}
public class AutoCloseableApi {
    public static void main(String[] args) {
        try (NetMessage nm = new NetMessage("Hello World")) {
            nm.send();
        } catch (Exception e) {}
    }
}

Runtime类

此图来源于李兴华老师
//获取Runtime类对象
public class RuntimeApi {
    public static void main(String[] args) {
        Runtime run = Runtime.getRuntime();
        System.out.println(run.availableProcessors());
    }
}
//观察内存状态
public class RuntimeApi {
    public static void main(String[] args) throws Exception {
        Runtime run = Runtime.getRuntime();
        System.out.println("【1】MAX_MEMORY " + run.maxMemory());
        System.out.println("【1】TOTAL_MEMORY " + run.totalMemory());
        System.out.println("【1】FREE_MEMORY " + run.freeMemory());
        String str = "";
        for (int x = 0; x < 30000; x ++) {
            str += x;
        }
        System.out.println("【2】MAX_MEMORY " + run.maxMemory());
        System.out.println("【2】TOTAL_MEMORY " + run.totalMemory());
        System.out.println("【2】FREE_MEMORY " + run.freeMemory());
        run.gc();
        System.out.println("【3】MAX_MEMORY " + run.maxMemory());
        System.out.println("【3】TOTAL_MEMORY " + run.totalMemory());
        System.out.println("【3】FREE_MEMORY " + run.freeMemory());
    }
}

System类

//操作耗时的统计
public class SystemApi {
    public static void main(String[] args) {
        long start = System.currentTimeMillis();
        String str = "";
        for (int x = 0; x < 30000; x ++) {
            str += x;
        }
        long end = System.currentTimeMillis();
        System.out.println(end - start);
    }
}

Cleaner类

//传统回收
import java.lang.ref.Cleaner;
class Member {
    public Member() {
        System.out.println("对象产生");
    }
    @Override
    protected void finalize() throws Throwable {
        System.out.println("对象回收");
        throw new Exception("程序继续执行");
    }
}
public class CleanerApi {
    public static void main(String[] args) {
        Member mb = new Member();
        mb = null;
        System.gc();
        System.out.println("程序结束");
    }
}

//Cleaner回收
class Member implements Runnable {
    public Member() {
        System.out.println("对象产生");
    }
    @Override
    public void run() {
        System.out.println("对象回收");
    }
}
class MemberCleaning implements AutoCloseable {
    public static final Cleaner cleaner = Cleaner.create();
    private Member member;
    private Cleaner.Cleanalbe cleanalbe;
    public MemberCleaning() {
        this.member = member;
        this.cleanalbe = this.cleaner.register(this, this.member);
    }
    @Override
    public void close() throws Exception {
        this.cleanalbe.clear();
    }
}
public class CleanerApi {
    public static void main(String[] args) throws Exception {
        try(MemberCleaning mc = new MemberCleaning()) {

        } catch (Exception e) {}
    }
}

对象克隆

class MemberClone implements Cloneable {
    private String name;
    private int age;
    public MemberClone(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();
    }
}
public class ObjectClone {
    public static void main(String[] args) throws Exception {
        MemberClone mcA = new MemberClone("张三", 18);
        MemberClone mcB = (MemberClone) mcA.clone();
        System.out.println(mcA);
        System.out.println(mcB);
    }
}
上一篇 下一篇

猜你喜欢

热点阅读