我们知道String每修改一次就会创建一个新的String的对象,这样非常浪费内存空间,所以在实际开发中可能会使用StringBuilder或者StringBuffer(线程安全)来解决内存浪费的问题。
首先看一下String的源码底层是char字符数组实现,被final修饰符修饰因此不可改变
再看下StringBuilder的源码,发现继承了AbstractStringBuilder这个类
在AbstractStringBuilder中看到了char数组,未被final修饰,这就是StringBuilder为什么可以更改的原因,底层实现也是char数组。
以下根据几个实际的例子来解释String和StringBuilder使用操作内存的原理
例1:请试着判断下面程序的输出结果
//例1
public class Practice1 {
public static void main(String[] args) {
String str1 = "hello";
String str2 = "world";
test1(str1, str2);
System.out.println(str1 +"," + str2);
StringBuilder sb1 = new StringBuilder("hello");
StringBuilder sb2 = new StringBuilder("world");
test2(sb1, sb2);
System.out.println(sb1 + "," + sb2);
}
public static void test1(String str1, String str2){
str1 += str2;
str2 += str1;
}
public static void test2(StringBuilder sb1, StringBuilder sb2){
sb1 = sb1.append(sb2);
sb2 = sb2.append(sb1);
}
}
输出结果 :hello,world
helloworld,worldhelloworld
根据程序可画出如下的内存图(地址均为虚构便于理解)
String:根据图片就可以很清晰的看出因为修改了String所以在堆中创建了新的对象,因此在test1方法中字符串指向的地址值发生了改变。0x111->0x333,0x222->0x444,但是main主方法中两个字符串指向的地址值并没有改变,所以最终的打印结果并没有发生改变。
StringBuilder:在堆中创建了2个StringBuilder对象地址分别为0x555和0x666。保证每次操作的都是同一个StringBuilder对象。
sb1 = sb1.append(sb2);
sb1指向的地址值并没有改变,因此操作0x555地址中StringBuilder对象的值改变为helloworld
sb2 = sb2.append(sb1);
sb2指向的地址值没有改变,操作的仍是0x666地址中的StringBuilder对象,append返回的sb1指向的地址值0x555中StringBuilder对象的值改变为了helloworld因此结果为worldhelloworld
例2:请试着判断下面程序的输出结果
思考:将sb2和sb1调换一下位置会发生什么结果呢?
//例2
public class Practice2 {
public static void main(String[] args) {
String str1 = "hello";
String str2 = "world";
test1(str1, str2);
System.out.println(str1 +"," + str2);//hello world
StringBuilder sb1 = new StringBuilder("hello");
StringBuilder sb2 = new StringBuilder("world");
test2(sb1, sb2);
System.out.println(sb1 + "," + sb2);
}
public static void test1(String str1, String str2){
str1 += str2;
str2 += str1;
}
public static void test2(StringBuilder sb1, StringBuilder sb2){
sb2 = sb1.append(sb2);
sb1 = sb2.append(sb1);
}
}
输出结果:hello,world
helloworldhelloworld,world
可以看出输出结果发生了改变,我们根据程序画出内存图
StringBuilder:
sb2 = sb1.append(sb2);
这次sb2的地址值发生了改变,指向了sb1指向的地址0x666->0x555,所以操作的是0x555地址中的StringBuilder对象,因此结果为helloworld
sb1 = sb2.append(sb1);
sb1指向sb2已经更改过的地址0x555,append返回的sb1的地址也为0x555,所以结果为helloworldhelloworld
在这过程中并没有操作到0x666地址中的StringBuilder对象,因此值并没有发生变化仍为world