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

1.什么是不可变的

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

2.如何保证不可变

1>类添加final修饰符,保证类是不可以被继承的;类继承会破坏类的不可变机制,只要覆盖父类的成员方法,并且在里面修改成员变量的值,那么所有子类以父类的形式出现的地方,类的属性都会被修改掉

2>类成员属性设置为private,final的;这样可以保证成员属性是不可变的,但是仅仅这样还不够,因为如果成员变量是引用对象的话,可以改变引用对象的成员变量;通过第四点可以避免这一点;

3>不提供修改成员变量的方法,比如setter;

4>通过构造器构造对象,并进行深拷贝;如果是直接通过引用传入的对象直接赋值给成员,还是可以通过修改外部引用变量导致改变内部变量的值;例如

public class ImmutabloeDemo {
    private final int[] myArray;

    public ImmutabloeDemo(int[] array) {
        this.myArray = array;
    }
}

这种方式,在外部修改array的值,内部的myArray也会修改了;为了保证外部的修改不会影响内部,可以采用深度clone的方法创建一个新内存来保存传入的值。

public class ImmutabloeDemo {
    private final int[] myArray;

    public ImmutabloeDemo(int[] array) {
        this.myArray = array.clone();
    }
}

5.同样,在getter方法中,不返回对象本身,只返回对象的克隆。

3.string怎么保证不可变的

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">
     * 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);
    }

1>String类是finaly的,不允许继承

2.成员变量value是private,final的

3.value没有setter方法

4.构造方法,是通过克隆的方式来构造的

5.返回value时,通过克隆的方式返回

public char[] toCharArray() {
    // Cannot use Arrays.copyOf because of class initialization order issues
    char result[] = new char[value.length];
    System.arraycopy(value, 0, result, 0, value.length);
    return result;
}
4.string类为不可变对象的好处

1>字符串常量池的需要

String one = "someString";
String two = "someString";

这两个对象指向同一个内存

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

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

2.线程安全的考虑

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

3.支持hash缓存

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

/** Cache the hash code for the string */
private int hash; // Default to 0
5.String比较

一般对象继承子Object类,equals方法使用==来比较对象的地址是否相同,特殊情况下,部分类重写equals方法,比如String对象比较相同时循环数组中每个字符串来判断是否相同的

public boolean equals(Object anObject) {
    if (this == anObject) {
        return true;
    }
    if (anObject instanceof String) {
        String anotherString = (String)anObject;
        int n = value.length;
        if (n == anotherString.value.length) {
            char v1[] = value;
            char v2[] = anotherString.value;
            int i = 0;
            while (n-- != 0) {
                if (v1[i] != v2[i])
                    return false;
                i++;
            }
            return true;
        }
    }
    return false;
}

所以

String a1 = new String("haha");
String a2 = new String("haha");
System.out.println(a1 == a2);

结果为false,因为new操作会在堆中常见两个对象出来。

String one = "someString";
String two = "someString";
System.out.println(one==two);

返回结果为true,因为都是常量池中的同一个对象,所以内存地址是一样的

String a  = "ABC";
String b = "AB";
String c = b + "C";
System.out.println( a == c );//false

a和b在编译时就确定了,c是变量。

6.string直接赋值和new的区别

string str1="ABC";可能会创建一个对象,也可能不创建对象,如果“ABC”这个字符串在string 池里不存在,就会创建一个对象,然后str1指向这个内存地址,无论以后用这种方式创建多少个值为“ABC”的字符串对象,始终只有一个内存被分配;

string str2 = new String("ABC");至少创建一个对象,也可能2个;用到new关键字,肯定会在堆中创建一个str2的String 对象,同时如果这个字符串在string池中不存在,会在stirng池中创建一个String对象,值为“ABC”;