分析Java中的String、StringBuilder以及StringBuffer

目录

1. String类

2.StringBuffer

3.StringBuilder

4.三者之间的比较


1. String类

  1. 从源码中可以看出,String类是final类,也即意味着String类不能被继承,并且它的成员方法都默认为final方法。在Java中,被final修饰的类是不允许被继承的,并且该类中的成员方法都默认为final方法。
  2. private final char value[];指明String底层是利用字符数组来保存字符串的。
  3. String str="hello"和String str=new String("hello")的区别
public final class String
    implements java.io.Serializable, Comparable<String>, CharSequence {
    /** The value is used for character storage. */
    private final char value[];

    /** 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 = -6849794470754667710L;

    /**
     * Class String is special cased within the Serialization Stream Protocol.
     *
     * A String instance is written into an ObjectOutputStream according to
     * <a href="{@docRoot}/../platform/serialization/spec/output.html">
     * Object Serialization Specification, Section 6.2, "Stream Elements"</a>
     */
    private static final ObjectStreamField[] serialPersistentFields =
        new ObjectStreamField[0];

    /**
     * Initializes a newly created {@code String} object so that it represents
     * an empty character sequence.  Note that use of this constructor is
     * unnecessary since Strings are immutable.
     */
    public String() {
        this.value = "".value;
    }

    /**
     * Initializes a newly created {@code String} object so that it represents
     * the same sequence of characters as the argument; in other words, the
     * newly created string is a copy of the argument string. Unless an
     * explicit copy of {@code original} is needed, use of this constructor is
     * unnecessary since Strings are immutable.
     *
     * @param  original
     *         A {@code String}
     */
    public String(String original) {
        this.value = original.value;
        this.hash = original.hash;
    }

    .....

2.StringBuffer

  1. 线程安全的可变字符序列。一个类似于 String 的字符串缓冲区,但不能修改。虽然在任意时间点上它都包含某种特定的字符序列,但通过某些方法调用可以改变该序列的长度和内容。

    可将字符串缓冲区安全地用于多个线程。可以在必要时对这些方法进行同步,因此任意特定实例上的所有操作就好像是以串行顺序发生的,该顺序与所涉及的每个线程进行的方法调用顺序一致。

    StringBuffer 上的主要操作是 appendinsert 方法,可重载这些方法,以接受任意类型的数据。每个方法都能有效地将给定的数据转换成字符串,然后将该字符串的字符追加或插入到字符串缓冲区中。append 方法始终将这些字符添加到缓冲区的末端;而 insert 方法则在指定的点添加字符。

  2. 常用方法

     StringBuffer append(int i)
              将 int 参数的字符串表示形式追加到此序列。
     int capacity()
              返回当前容量。
     char charAt(int index)
              返回此序列中指定索引处的 char 值。
     StringBuffer delete(int start, int end)
              移除此序列的子字符串中的字符。
     int indexOf(String str)
              返回第一次出现的指定子字符串在该字符串中的索引。
     StringBuffer insert(int offset, int i)
              将 int 参数的字符串表示形式插入此序列中。
     int length()
              返回长度(字符数)。
     StringBuffer replace(int start, int end, String str)
              使用给定 String 中的字符替换此序列的子字符串中的字符。
     StringBuffer reverse()
              将此字符序列用其反转形式取代。
     CharSequence subSequence(int start, int end)
              返回一个新的字符序列,该字符序列是此序列的子序列。
     String substring(int start, int end)
              返回一个新的 String,它包含此序列当前所包含的字符子序列。
     String toString()
              返回此序列中数据的字符串表示形式。

3.StringBuilder

  1. 线程不安全的一个可变的字符序列。此类提供一个与 StringBuffer 兼容的 API,但不保证同步。该类被设计用作 StringBuffer 的一个简易替换,用在字符串缓冲区被单个线程使用的时候(这种情况很普遍)。如果可能,建议优先采用该类,因为在大多数实现中,它比 StringBuffer 要快

4.三者之间的比较

  1. 运行速度:StringBuilder > StringBuffer > String
  2. 线程安全:StringBuilder是线程不安全的,而StringBuffer是线程安全的
  3. 应用场景:

String:适用于少量的字符串操作的情况

StringBuilder:适用于单线程下在字符缓冲区进行大量操作的情况

StringBuffer:适用多线程下在字符缓冲区进行大量操作的情况

猜你喜欢

转载自blog.csdn.net/m0_38109046/article/details/88987031