java---System 系统类、字符串类(String)、StringBuilder(字符缓冲区)、StringBuffer

目录

一、System 系统类

二、字符串类(String)

三、StringBuilder(字符缓冲区)

四、StringBuffer


一、System 系统类

常用方法:  

 

//学习数组的时候,自己写过数组拷贝的代码
 public static native void arraycopy(Object src,  int  srcPos,Object dest, int destPos, int length);

//查询当前系统时间
System.currentTimeMillis();
public static native long currentTimeMillis();

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

 

二、字符串类(String)

 

    • 构造器 -> 字段 -> 方法

      • 构造器

        //无参构造器
        public String() {
            //做了数组初始化
            this.value = new char[0];
        }
        //带一个参数
        public String(String original) {
            //给char 数组赋值
            this.value = original.value;
            //缓存String 的 hash 值
            this.hash = original.hash;
        }
        //传递 char 类型数组
        public String(char value[]) {
            this.value = Arrays.copyOf(value, value.length);
        }
      • 字段

        /** The value is used for character storage. */
        //存储字符的数组
        private final char value[];
        ​
        /** Cache the hash code for the string */
        //缓存 hash code
        private int hash; // Default to 0
      • 方法

        //查看字符串长度
        public int length() {
            return value.length;
        }
        //判断是否为空
        public boolean isEmpty() {
            return value.length == 0;
        }
        //返回一个索引对应字符值
        public char charAt(int index) {
            return value[index];
        }
        //判断是否包含一个字符
        public boolean contains(CharSequence s) {
            return indexOf(s.toString()) > -1;
        }
        //获取byte[]
        public byte[] getBytes() {
            return StringCoding.encode(value, 0, value.length);
        }
        //以什么开头
        public boolean startsWith(String prefix) {
            return startsWith(prefix, 0);
        }
        //以什么结尾
        public boolean endsWith(String suffix) {
            return startsWith(suffix, value.length - suffix.value.length);
        }
        //equals 方法
        equals(Object obj)
        //以什么什么断开
        public String[] split(String regex) {
            return split(regex, 0);
        }
        //去掉空字符串 “   abc   ”
        public String trim() {}

 

三、StringBuilder(字符缓冲区)

 

  1. StringBuilder 也是final 修饰的不可以不继承

  2. 里面的 char[] 数组不是使用 final 修饰的,可以替换

  3. 构造器

    //默认的数组长度为16
    public StringBuilder() {
        super(16);
    }
    //指定容量
    public StringBuilder(int capacity) {
        super(capacity);
    }
  4. 方法

    //做char[] 的拷贝或者拼接
    @Override
    public StringBuilder append(String str) {
        super.append(str);
        return this;//放回当前对象
    }
    ​
    //将 StringBuilder 对象转换成字符串对象
    @Override
    public String toString() {
        // Create a copy, don't share the array
        return new String(value, 0, count);
    }
  5. 字段

    /**
    * The value is used for character storage.
    */
    char[] value;
    ​
    /**
    * The count is the number of characters used.
    */
    int count;

 

四、StringBuffer

  1. 使用synchronized修饰,是线程安全

  2. 注意:

    • StringBuilder 性能最好 > StringBuffer > String

    • StringBuffer 是线程安全的

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值