String、StringBuffer、StringBuilder之间的区别(浅谈)

文章目录

基本概念

String

Strings are constant; their values cannot be changed after they are created. String buffers support mutable strings. Because String objects are immutable they can be shared.

字符串不变; 它们的值在创建后不能被更改。 字符串缓冲区支持可变字符串。 因为String对象是不可变的,它们可以被共享。

StringBuffer

A thread-safe, mutable sequence of characters. A string buffer is like a String, but can be modified. At any point in time it contains some particular sequence of characters, but the length and content of the sequence can be changed through certain method calls.

String buffers are safe for use by multiple threads. The methods are synchronized where necessary so that all the operations on any particular instance behave as if they occur in some serial order that is consistent with the order of the method calls made by each of the individual threads involved.

线程安全,可变的字符序列。 字符串缓冲区就像一个String,但可以修改。 在任何时间点,它包含一些特定的字符序列,但可以通过某些方法调用来更改序列的长度和内容。

字符串缓冲区可以安全地被多个线程使用。 这些方法在必要时进行同步,以便任何特定实例上的所有操作都按照与所涉及的各个线程所执行的方法调用顺序一致的顺序发生。

StringBuilder

A mutable sequence of characters. This class provides an API compatible with StringBuffer, but with no guarantee of synchronization. This class is designed for use as a drop-in replacement for StringBuffer in places where the string buffer was being used by a single thread (as is generally the case). Where possible, it is recommended that this class be used in preference to StringBuffer as it will be faster under most implementations.

一个可变的字符序列。 此类提供与StringBuffer的API,但不保证同步。 此类设计用作简易替换为StringBuffer在正在使用由单个线程字符串缓冲区的地方(如通常是这种情况)。 在可能的情况下,建议使用这个类别优先于StringBuffer ,因为它在大多数实现中将更快。

从上面看出,

  • String:字符串不可变序列,只能是共享。

    当改变一个String对象的时候,JVM会重新创建一个新的对象来存储这个对象,原来的对象并不改变。

  • StringBuffer:字符串可变序列,线程安全。

  • StringBuilder:字符串可变序列,线程不安全。

注:由此可见,速度是:String < StringBuffer < StringBuilder



实例

下面代码中,没有用到hashCode(),toString() 方法,用的是System类中的identityHashCode()

public static int identityHashCode()

Returns the same hash code for the given object as would be returned by the default method hashCode(), whether or not the given object’s class overrides hashCode(). The hash code for the null reference is zero.

返回与默认方法hashCode()返回的给定对象相同的哈希码,无论给定对象的类是否覆盖了hashCode()。 空引用的哈希码为零。

注:toString()、hashCode() 本来表示的是对象的地址,对象的哈希值(对象的唯一标识),一旦被重写之后,这些将表达的意思就变了。

/**
 * String、StringBuffer、StringBuilder之间的区别
 * 通过,比较原先字符串对象的地址 跟修改字符串对象的地址,来判断是否发生变化
 * 
 * @author xiaomingxing
 * @create 2019/12/9 20:20
 */
public class Demo {
    public static void main(String[] args) {
        String str = "初始化";
        System.out.println(System.identityHashCode(str));
        str = str + "追加内容";
        System.out.println(System.identityHashCode(str));

        System.out.println();

        StringBuffer stringBuffer = new StringBuffer("初始化");
        System.out.println(System.identityHashCode(stringBuffer));
        stringBuffer = stringBuffer.append("追加内容");
        System.out.println(System.identityHashCode(stringBuffer));

        System.out.println();

        StringBuilder stringBuilder = new StringBuilder("初始化");
        System.out.println(System.identityHashCode(stringBuilder));
        stringBuilder = stringBuilder.append("追加内容");
        System.out.println(System.identityHashCode(stringBuilder));
    }
}


运行结果:
Snipaste_2019-12-09_20-45-15.png



总结

见下表:

特点 适用场景
String 对象不可变 频繁改变(拼接)一个字符串时
StringBuffer 对象不可变 多线程下频繁改变(拼接)一个字符串时
StringBuilder 对象不可变 单线程下频繁改变(拼接)一个字符串时
发布了10 篇原创文章 · 获赞 30 · 访问量 1393

猜你喜欢

转载自blog.csdn.net/qq_35502243/article/details/103475124
今日推荐