ITPub博客

首页 > 数据库 > Oracle > 原来只想简单看一下String源码,没想到整理了这么多知识点

原来只想简单看一下String源码,没想到整理了这么多知识点

Oracle 作者:椰子奶咖 时间:2021-02-01 23:34:00 0 删除 编辑

不知道大家有没有这样得经历,就是无意中点进去得一个业面,然后钻到里面浏览了好久,我就是这样得,今天无意中,ctrl+左键,就点进了string得源码,正好今天下午没啥事,就在里面看一下,没想到,下次缓过来,就是我同事拍我让我去吃饭,哈哈哈哈,不过好处就是,我这边也整理了一些string类得知识点,也分享给大家,整理得不好还望海涵

文章首发个人公众号:Java架构师联盟,每日更新技术好文

一、String类

想要了解一个类,最好的办法就是看这个类的实现源代码,来看一下String类的源码:

public final class String

    implements java.io.Serializable, Comparable<String>, CharSequence{

    /** The value is used for character storage. */

    private final char value[];

 

    /** The offset is the first index of the storage that is used. */

    private final int offset;

 

    /** The count is the number of characters in the String. */

    private final int count;

 

    /** Cache the hash code for the string */

    private int hash; // Default to 0

 

    /** use serialVersionUID from JDK 1.0.2 for interoperability */

    private static final long serialVersionUID = -6849794470754 远程桌面 667710L;

 

    ........

}

从上面可以看出几点:

1)String类是final类,也即意味着String类不能被继承,并且它的成员方法都默认为final方法。在Java中,被final修饰的类是不允许被继承的,并且该类中的成员方法都默认为final方法。

2)上面列举出了String类中所有的成员属性,从上面可以看出String类其实是通过char数组来保存字符串的。

下面再继续看String类的一些方法实现:

public String substring(int beginIndex, int endIndex) {

    if (beginIndex < 0) {

        throw new StringIndexOutOfBoundsException(beginIndex);

    }

    if (endIndex > count) {

        throw new StringIndexOutOfBoundsException(endIndex);

    }

    if (beginIndex > endIndex) {

        throw new StringIndexOutOfBoundsException(endIndex - beginIndex);

    }

    return ((beginIndex == 0) && (endIndex == count)) ? this :

        new String(offset + beginIndex, endIndex - beginIndex, value);

}

public String concat(String str) {

    int otherLen = str.length();

    if (otherLen == 0) {

        return this;

    }

    char buf[] = new char[count + otherLen];

    getChars(0, count, buf, 0);

    str.getChars(0, otherLen, buf, count);

    return new String(0, count + otherLen, buf);

}

public String replace(char oldChar, char newChar) {

    if (oldChar != newChar) {

        int len = count;

        int i = -1;

        char[] val = value; /* avoid getfield opcode */

        int off = offset;   /* avoid getfield opcode */

 

        while (++i < len) {

        if (val[off + i] == oldChar) {

            break;

        }

        }

        if (i < len) {

        char buf[] = new char[len];

        for (int j = 0 ; j < i ; j++) {

            buf[j] = val[off+j];

        }

        while (i < len) {

            char c = val[off + i];

            buf[i] = (c == oldChar) ? newChar : c;

            i++;

        }

        return new String(0, len, buf);

        }

    }

    return this;

}

从上面的三个方法可以看出,无论是sub操、concat还是replace操作都不是在原有的字符串上进行的,而是重新生成了一个新的字符串对象。也就是说进行这些操作后,最原始的字符串并没有被改变。

在这里要永远记住一点:“String对象一旦被创建就是固定不变的了,对String对象的任何改变都不影响到原对象,相关的任何change操作都会生成新的对象”。

二、字符串常量池

我们知道字符串的分配和其他对象分配一样,是需要消耗高昂的时间和空间的,而且字符串我们使用的非常多。JVM为了提高性能和减少内存的开销,在实例化字符串的时候进行了一些优化:使用字符串常量池。每当我们创建字符串常量时,JVM会首先检查字符串常量池,如果该字符串已经存在常量池中,那么就直接返回常量池中的实例引用。如果字符串不存在常量池中,就会实例化该字符串并且将其放到常量池中。由于String字符串的不可变性我们可以十分肯定常量池中一定不存在两个相同的字符串(这点对理解上面至关重要)。

Java中的常量池,实际上分为两种形态:静态常量池和运行时常量池。 所谓静态常量池,即*.class文件中的常量池,class文件中的常量池不仅仅包含字符串(数字)字面量,还包含类、方法的信息,占用class文件绝大部分空间。 而运行时常量池,则是jvm虚拟机在完成类装载操作后,将class文件中的常量池载入到内存中,并保存在方法区中,我们常说的常量池,就是指方法区中的运行时常量池。

来看下面的程序:

String a = "chenssy";String b = "chenssy";

a、b和字面上的chenssy都是指向JVM字符串常量池中的"chenssy"对象,他们指向同一个对象。

String c = new String("chenssy");

new关键字一定会产生一个对象chenssy(注意这个chenssy和上面的chenssy不同),同时这个对象是存储在堆中。所以上面应该产生了两个对象:保存在栈中的c和保存堆中chenssy。但是在Java中根本就不存在两个完全一模一样的字符串对象。故堆中的chenssy应该是引用字符串常量池中chenssy。所以c、chenssy、池chenssy的关系应该是:c--->chenssy--->池chenssy。


来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/69993988/viewspace-2755273/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2021-01-26

  • 博文量
    7
  • 访问量
    4617