Java String

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

Java String

​ 共饮一杯无   ​   2022-06-19 我要评论

前言:

String 是 Java 语言非常基础和重要的类,提供了构造和管理字符串的各种基本逻辑。它是典型的 Immutable 类,被声明成为 final class,所有属性也都是 final 的。也由于它的不可变性,类似拼接、裁剪字符串等动作,都会产生新的 String 对象。由于字符串操作的普遍性,所以相关操作的效率往往对应用性能有明显影响。 StringBuffer 是为解决上面提到拼接产生太多中间对象的问题而提供的一个类,我们可以用 append 或者 add 方法,把字符串添加到已有序列的末尾或者指定位置。StringBuffer 本质是一个线程安全的可修改字符序列,它保证了线程安全,也随之带来了额外的性能开销,所以除非有线程安全的需要,不然还是推荐使用它的后继者,也就是 StringBuilder。 StringBuilder 是 Java 1.5 中新增的,在能力上和 StringBuffer 没有本质区别,但是它去掉了线程安全的部分,有效减小了开销,是绝大部分情况下进行字符串拼接的首选。

String类为什么是immutable(不可变的)

不可变类指的是对象一旦创建成功,就无法改变对象的值。JDK中很多类设计为不可变的Integer,Long和String等。相对应的改法中大多是可变类,创建成功后可以动态修改成员变量的属性值;

如何保证不可变

  • 类添加final修饰符,保证类是不可以被继承的;类继承会破坏类的不可变机制,只要覆盖父类的成员方法,并且在里面修改成员变量的值,那么所有子类以父类的形式出现的地方,类的属性都会被修改掉
  • 类成员属性设置为private,final的;这样可以保证成员属性是不可变的,但是仅仅这样还不够,因为如果成员变量是引用对象的话,可以改变引用对象的成员变量;通过第四点可以避免这一点;
  • 不提供修改成员变量的方法,比如setter;
  • 通过构造器构造对象,并进行深拷贝;如果是直接通过引用传入的对象直接赋值给成员,还是可以通过修改外部引用变量导致改变内部变量的值;

String的源码如下:

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" rel="external nofollow" >
     * 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;
    }

    /**
     * Allocates a new {@code String} so that it represents the sequence of
     * characters currently contained in the character array argument. The
     * contents of the character array are copied; subsequent modification of
     * the character array does not affect the newly created string.
     *
     * @param  value
     *         The initial value of the string
     */
    public String(char value[]) {
        this.value = Arrays.copyOf(value, value.length);
    }

通过源码可以看出来String是如何保证不可变的:

  • String类是finaly的,不允许继承
  • 成员变量value是private,final的
  • value没有setter方法
  • 构造方法,是通过克隆的方式来构造的
  • 返回value时,通过克隆的方式返回

string类为不可变对象的好处

字符串常量池的需要:

String aaa= "someString"; String bbb = "someString"; 这两个对象指向同一个内存,字符串常量池的好处是,在大量使用字符串的时候,可以节省内存,提供效率;如果String是可变对象,那么修改了一个,其他引用的地方全部发生变化了。

线程安全的考虑:

在并发场景下,多个线程同时读一个资源,不会引发竞争,但是同时写操作会引发竞争,string的不可变特点,所以线程安全的。

支持hash缓存:

因为字符串是不可变的,所以创建的时候hash被缓存下来了,不需要重新计算,使得字符串很适合做Map的键,处理速度要快过其他的对象。

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们