day17 String类

String

String类的特点

  1. String 属于引用数据类型,不是基本数据类型

  2. String对象存储在常量池,并且是不可变的。不可变,指指向的对象不可变,并非对象的内容不可变。

  3. JVM底层优化机制

    String s = "a" + "b";  // 这样的字符串常量相加,JVM底层中有优化,会直接生成"ab" 字符串保存在常量池
    
    String s = "b"
    String s1 = "a" + s; //这种形式没有优化,常量池种仍然有 "b" 和 "ab"
    
  4. 比较String类用equals方法,不要用==比较

  5. String类的底层是一个final修饰的value数组,在jdk1.8之前是char[]类型,在1.9之后更变为byte[]类型。此举节省了字符串内存的占用。char类型是UTF-8编码的,占用两个字节。在jdk1.9优化成了了byte数组,编码方式采用Latin-1(单字节编码) 和UTF-16(双字节编码)来表示。为区分编码方式,增加了private final byte coder;这一属性。通过使用byte[]将内存由原本的中英文都占两字节优化成了中文占两字节,英文占一字节。
    选择编码的方式如下:

    public String(int[] codePoints, int offset, int count) {
            checkBoundsOffCount(offset, count, codePoints.length);
            if (count == 0) {
                this.value = "".value;
                this.coder = "".coder;
                return;
            }
            if (COMPACT_STRINGS) {
                byte[] val = StringLatin1.toBytes(codePoints, offset, count);
                if (val != null) {
                    this.coder = LATIN1;
                    this.value = val;
                    return;
                }
            }
            this.coder = UTF16;
            this.value = StringUTF16.toBytes(codePoints, offset, count);
    }
    

两种创建String对象的对比

  1. 常量池直接赋值
    先在常量池中寻找有无"abc",如果有,直接指向该地址;如果没有,则先创建"abc"再指向。
    String s = "abc";
    
  2. 调用构造器12new一个对象
    先在堆中创建空间,里面维护了value属性,指向常量池的"abc"空间。
    如果常量池中有"abc",直接指向该地址;如果没有,则先创建"abc"再指向。
    String s2 = new String("abc");
    

String类实现的接口

  1. String类实现了Serializable,说明String可以串行化(可以进行网络传输)
  2. String类实现了Comparable,说明String可以比较大小
  3. String类实现了CharSequence,说明String可以

String类的构造器重载

String(); // 无参构造器,value数组指向null
String(String original); // 用已有String对象来创建String对象
String(char[] value); // 用char数组来创建String对象
String(char[] value, int offset, int count); // 用value数组从,下标为offset的字符开始的count个字符来创建String对象
String(StringBuffer buffer); // 用StringBuffer类的对象来创建String对象
String(StringBuilder builder); // 用StringBuilder类的对象来创建String对象

String类的常用方法

  1. 获取字符串长度

    public int length()
    
  2. 获取下标为index位置的字符

    public char charAt(int index)
    
  3. 获取子串

    public String substring(int beginIndex)
    public String substring(int beginIndex, intendIndex)
    
  4. 字符串比较

    public int compareTo(String str)
    public int compareToIgnoreCase (String str)
    public boolean equals(Object obj)
    public boolean equalsIgnoreCase(String str)
    
  5. 查找子串

    public int indexOf(String str)
    public int indexOf(String str, intfromIndex)
    public int lastIndexOf(String str)
    public int lastIndexOf(String str, intfromIndex)
    
  6. 替换字符串

    public String replace(char oldChar, charnewChar)
    public String replaceFirst(String regex,String replacement)
    public String replaceAll(String regex,String replacement)
    
  7. 大小写转换

    public String toLowerCase()
    public String toUpperCase()
    
  8. 分割字符串

    public String[] split(String str)
    
  9. 删除两端空格

    public String trim()
    
  10. 基本数据类型转String类型(static method)

    public static String valueOf(char data[])
    public static String valueOf(char data[], int offset, int count)
    public static String valueOf(boolean b)
    public static String valueOf(char c)
    public static String valueOf(int i)
    public static String valueOf(long l)
    public static String valueOf(float f)
    public static String valueOf(double d)
    
  11. 转换成字符数组

    char[] toCharArray()
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值