String对象是否可变这个问题,是一个老问题,今天就详细分析一下。
1、String采用的是immutable 不可变的设计模式,String被final修饰。因此,String类也就无法被继承,方法不能被重写。String对象也就无法改变。至于为什么设计成不可变的。请看我之前的一篇关于String,StringBuilder和StringBuffer分析的文章,里面有详细的讲述。
2、一般的说String对象不可变,都是基于JDK API的。在String源码中可以发现,存放String对象的值的属性是final的。但是final修饰的属性值就一定不会被改变吗?答案当然是“可以的”。当我们绕过JDK时,我们可以通过反射区改变final修饰的属性的值。下面就来讲一下如何修改。
首先我们来先看一下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 = -6849794470754667710L;
......
}
从上面的String的源码中我们可以看到,String的值是存放在一个被final修饰叫做value[]的字符数组里面。我们要想改变String对象是
public class Test {
public static void main(String[] args) throws Exception {
String s="0123456789";
System.out.println("改变前:s=" + s);
Field f = s.getClass().getDeclaredField("value");//定义一个可以反射String中value的Field对象
f.setAccessible(true); //知识反射的对象在使用时取消java语言的访问检查
f.set(s, new char[]{'a', 'b', 'c'}); //将s的变量设置成abc
System.out.println("改变后:s=" + s);
}
}
打印结果:
改变前:s=0123456789
改变后:s=abc
结论:
String 类里有个 private final char value[]成员变量, String 类的值都是保存在这个value属性中。
可以通过反射来改变这个值,达到改变String类值的目的。同理,通过这种方式,我们可以改变自定义对象的私有成员变量。