Java2-Java常用类库(StringBuffer、CharSequence、AutoCloseable、Runtime、System)

1、StringBuffer类

(1)String 类

  • 两个常量池:静态常量池、运行时常量池;
  • 实例化对象直接赋值,保存在常量池便于复用;
  • 不利于对象内容修改;

(2)StringBuffer

  • 内容可修改;
  • StringBuffer实例化对象之后才可以使用,线程安全;
      //1-String
        String a = "11223344";
        String b = "11"+"22"+"33"+"44";
        System.out.println(a==b); //true
        //二者可以互相转换,"+"的本质就是append
        //2-StringBuffer
        //2-1追加
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append("你好").append(1).append(true);
        //2-2插入
        stringBuffer.insert(0,"66");
        //2-3范围删除
        stringBuffer.delete(1,2);
        //2-4内容反转
        stringBuffer.reverse();
        //2-5替换
        stringBuffer.replace(2,3,"替换");
        System.out.println(stringBuffer); //eu替换t1好你6

StringBufferStringBuilder
线程安全非线程安全

2、CharSequence接口

字符串序列:描述字符串的结构,本质就是一个字符串;string\stringbuffer\stringbulider都实现了该接口。只要存在字符串,就可以为CharSequence接口实例化。

       CharSequence charSequence = "123456789";
        //获取指定位置字符
        System.out.println(charSequence.charAt(1));//2
        //长度
        System.out.println(charSequence.length());//9
        //截取
        System.out.println(charSequence.subSequence(1,2));//2

3、AutoCloseable接口

自动资源关闭:主要用于资源开发时释放资源,自动关闭。
想要实现自动关闭,除了实现AutoCloseable接口,还需要结合异常处理语句才能完成。

package com.wxrem.controller;
 
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
 
/**
 * @author wxb
 * 奇妙巧妙关闭流 AutoCloseable
 * @date 2020-09-03 15:28
 */
public class AutoCloseableDemo {
    public static void main(String[] args) {
        try (AutoCloseableObjecct app = new AutoCloseableObjecct()) {
            System.out.println("--执行main方法--");
            System.out.println("--demo2--");
            app.demo2();
        } catch (Exception e) {
            System.out.println("--exception--");
        } finally {
            System.out.println("--finally--");
        }
//结果:
// --执行main方法--
//--demo2--
//--fileInputStream2--
//--close fileInputStream2--
//--finally--
    }
 
    //自己定义类 并实现AutoCloseable
    public static class AutoCloseableObjecct implements AutoCloseable {
        //其中close()方法是关闭流并且释放与其相关的任何方法,如果流已被关闭,那么调用此方法没有效果。
        @Override
        public void close() throws Exception {
            System.out.println("--close fileInputStream2--");
        }
 
        public static void demo2() {
 
            //JDK1.7之前,释放资源方式
//        FileInputStream fileInputStream = null;
//        try {
//            fileInputStream = new FileInputStream("");
//        } catch (FileNotFoundException e) {
//            e.printStackTrace();
//        } finally {
//            try {
//                fileInputStream.close();
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//        }
 
            //1.7之后,只要实现了AutoCloseable接口
            try (FileInputStream fileInputStream2 = new FileInputStream("F:\\server (2).log")) {
                System.out.println("--fileInputStream2--");
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
 
        }
 
    }
 
 
}

4、Runtime类

运行时状态:在一个JVM进程里面只允许提供一个Runtime对象,所以被默认私有化,故使用单例设计模式。

 //Runtime只允许存在一个,实例化对象只能通过getRuntime方法获取;
        Runtime runtime = Runtime.getRuntime();
        //获取CPU内核数
        System.out.println(runtime.availableProcessors()); //8
        //获取最大可用内存空间
        System.out.println(runtime.maxMemory());
        //获取可用内存空间
        System.out.println(runtime.totalMemory());
        //获取空闲内存空间
        System.out.println(runtime.freeMemory());
        //手工进行GC处理:void 类型
        runtime.gc();

GC:垃圾回收器,由系统自动调用的垃圾回收功能或者Runtime类中手工调用;

5、System类

//数组拷贝:public static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length
//        Object src : 原数组
//        int srcPos : 从元数据的起始位置开始
//       Object dest : 目标数组
//       int destPos : 目标数组的开始起始位置
//       int length  : 要copy的数组的长度
        byte[]  srcBytes = new byte[]{2,4,0,0,0,0,0,10,15,50};  // 源数组
        byte[] destBytes = new byte[5]; // 目标数组
        System.arraycopy(srcBytes,0,destBytes ,0,5);
        //获取当前日期时间数值:耗时统计
        System.currentTimeMillis();
        //GC,继续执行Runtime的gc方法;
        System.gc();
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值