深入理解String,StringBuffer,StringBuilder

前言

String类是Java中使用的非常频繁的一个类,也是在面试中被经常问到的一个地方。

Java中字符串的表示有三种方法,分别为String、StringBuffer、StringBuilder。它们的异同点也是在面试中的高频提问点。本片文章将会系统地梳理以下这三者的区别。

String类

看一下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;

   ......

}

从源码可以看出:

  1. String类由final修饰,意味着String不能被继承,并且它的成员方法默认都为final。在现在的Java SE版本,final只会用在确定不想让方法被覆盖的情况。
  2. String类其实是用char数组来保存字符串的。

再来看一下String类的一些方法实现:

public String substring(int beginIndex, int endIndex) {
   if (beginIndex < 0) {
       throw new StringIndexOutOfBoundsException(beginIndex);
   }
   if (endIndex > count) {
       throw new StringIndexOutOfBoundsException(endIndex);
   }
   if (beginIndex > endIndex) {
       throw new StringIndexOutOfBoundsException(endIndex - beginIndex);
   }
   return ((beginIndex == 0) && (endIndex == count)) ? this :
       new String(offset + beginIndex, endIndex - beginIndex, value);
   }

public String concat(String str) {
   int otherLen = str.length();
   if (otherLen == 0) {
       return this;
   }
   char buf[] = new char[count + otherLen];
   getChars(0, count, buf, 0);
   str.getChars(0, otherLen, buf, count);
   return new String(0, count + otherLen, buf);
   }

public String replace(char oldChar, char newChar) {
   if (oldChar != newChar) {
       int len = count;
       int i = -1;
       char[] val = value; /* avoid getfield opcode */
       int off = offset;   /* avoid getfield opcode */

       while (++i < len) {
       if (val[off + i] == oldChar) {
           break;
           }
       }
       if (i < len) {
       char buf[] = new char[len];
       for (int j = 0 ; j < i ; j++) {
           buf[j] = val[off+j];
       }
       while (i < len) {
           char c = val[off + i];
           buf[i] = (c == oldChar) ? newChar : c;
           i++;
       }
       return new String(0, len, buf);
       }
   }
   return this;

从上面的方法可以看出,每个方法都不是在原有的字符串上进行操作的,而是重新生成了一个新的字符串对象。也就是说,原来的字符串并没有被改变。

对String对象的任何改变都不会影响到原对象,相关的任何修改操作都会生成新的对象。

深入理解String、StringBuffer、StringBuilder

String str = "hello world"; 和 String str = new String("hello world");的区别

先看一个例子:

public class Main {

   public static void main(String[] args) {
       String str1 = "hello world";
       String str2 = new String("hello world");
       String str3 = "hello world";
       String str4 = new String("hello world");

       System.out.println(str1==str2);
       System.out.println(str1==str3);
       System.out.println(str2==str4);
   }
}

这段代码的输出结果为:

false
true
false

解析:在class文件中,有一部分是用来存储编译期间生成的字面常量以及符号引用,这部分叫做class文件常量池,在运行期间对应着方法区的运行常量池。

在上述代码中,String str1 = "hello world";和String str3 = "hello world"; 都在编译期间生成了
字面常量和符号引用,运行期间字面常量"hello world"被存储在运行时常量池。通过这种方式来将String对象跟引用绑定的话,JVM会先在运行时常量池查找是否存在相同的字面常量,如果存在,则直接引用指向已经存在的字面常量;否则在运行时常量池开辟一个空间来存储该字面常量,并将引用指向该字面常量。

而通过new关键字生成对象是在堆区进行的,在堆区进行对象生成过程是不会检测该对象是否存在的。因此通过new创建对象,一定创建的是不同的对象,即使字符串的内容相同。

String、StringBuilder的区别

看下面的代码:

public class Main{

    public static void main(String args[]){
        String str = "";
        for(int i = 0;i < 10000;i++){
            str+= "hello";
        }
    }
}

其中string+="hello";的过程相当于将原有的String变量指向的对象内容取出与"hello"作字符串相加操作,再存进另一个新的String对象当中,再让String变量指向新生成的对象。具体的细节可以查看反编译的字节码:

006ozJEaly1ghh1yn76mcj30mc0iln2m.jpg

其中第8行到35行是整个循环的执行过程,并且每次循环都会new出一个StringBuilder对象,然后进行append操作,最后通过toString方法返回String对象。也就是说这个循环过程new出了10000个对象,如果这些对象没有被回收,将会造成非常大的内存资源浪费。

从上面还可以看出,String+="hello";的操作会被JVM优化成以下代码:

StringBuilder str = new StringBuilder(String);
str.append("hello");
str.toString();

再来看下面的代码:

public class Main{

    public static void main (String args[]){
        StringBuilder str = new StringBuilder();
        for(int i=0;i<10000;i++){
            str.append("hello");
        }
    }
}

反编译字节码文件得到:

006ozJEaly1ghh2dt7qrvj30m80f777x.jpg

从字节码可以明显看出,循环代码从13行开始到27行结束,并且new操作只执行了一次,也就是说只生成了一个对象,append操作是在原有的对象上进行的,因此循环了10000次后,占用的内存资源要比上面的小的多。

StringBuilder和StringBuffer的区别

其实,二者拥有的成员属性和成员方法基本相同,区别是StringBuffer类的成员方法前面多了一个关键字:synchronized,这个关键字在多线程访问时起到了安全保护作用,也就是说StringBuffer是线程安全的。

二者的insert方法:

public StringBuilder insert(int index, char str[], int offset, int len){
     super.insert(index, str, offset, len);
     return this;
 }

public synchronized StringBuffer insert(int index,char str[],int offset,int len){
    super.insert(index,str,offset,len);
    return this;
}

三者的执行效率

从运行速度上来说,速度的快慢为:StringBuilder > StringBuffer > String

String慢的原因为String是字符串常量,不可修改;而另外两者是字符串变量,可以修改。
当然以上的速度排序也不是绝对的,比如String str = "hello" + "world"; 就比StringBuilder str2 = new StringBuilder.append("hello").append("world");的执行速度快。

从线程安全上来说,StringBuilder是线程不安全的,而StringBuffer是线程安全的。

如果要进行的操作是多线程的,那么就要使用StringBuffer,单线程的情况下还是建议使用速度较快的StringBuilder。

总结

String:适用于少量的字符串操作的情况
StringBuilder:适用于单线程下在字符缓冲区进行大量操作的情况
StringBuffer:适用于多线程下在字符缓冲区进行大量操作的情况

一些经典面试题

下面这段代码的运行结果是什么?(一)

String a = "hello2";
String b = "hello" + 2;
System.out.println((a==b));

答案是true,因为"hello"+2在编译期间就已经优化成了"hello2",因此在运行期间,a和b变量指向的是同一个对象。

下面这段代码的运行结果是什么?(二)

String a = "hello2";
String b = "hello";
String c = b + 2;
System.out.println((a==c));

答案是false,由于有符号引用的存在,所以String c = b + 2;这段代码不会在编译期间被优化,不会把b+2当作字面常量来处理,因此这种方式生成的对象事实上是保存在堆上的。因此a和c指向的不是同一个对象。

下面这段代码的运行结果是什么?(三)

String a = "hello";
final String b = "hello";
String c = b + 2;
System.out.println((a==c));

答案是true。对于被final修饰的变量,会在class文件常量池中保存一个副本,也就是说不会通过连接进行访问,对final变量的访问在编译期间都会被直接替代为真实的值。所以String c = b + 2;在编译期间会被优化成String c = "hello" + 2;

下面这段代码的运行结果是什么?(四)

public class Main{
    public static void main(String args[]){
        String a = "hello2";
        final String b = getHello();
        String c = b + 2;
        System.out.println((a==c));
    }
}

public static String getHello(){
    return "hello";
}

答案是false,虽然b被final所修饰,但是赋值的方式是通过方法调用返回的,它的值只能在运行期间确定,因此a和c指向的不是同一个对象。

下面这段代码的输出结果是什么?(五)

public class Main{
    public static void main(String args[]){
        String a = "hello";
        String b = new String("hello");
        Strint c = new String("hello");
        String d = b.intern();

        System.out.println(a==b);
        System.out.println(a==c);
        System.out.println(b==d);
        System.out.println(a==d);
    }
}

输出结果为:

false
false
false
true

解析:其中前两个较为简单,new生成的变量地址一定是不同的。在String类中,intern()方法是一个本地方法。简单来说就是intern用来返回常量池中的某字符串,如果常量池中已经存在该字符串,则直接返回常量池中该对象的引用。否则,在常量池中加入该对象,然后返回引用。在上述代码中字符串池中已经存在"hello",则直接返回引用,因此返回false与true。

String str = new String("abc");创建了多少个对象?

这个问题在很多书籍中以及大公司的笔试题都会遇到,网上流传的答案一般都是2个对象,其实是不准确的。如果问题换成是"在运行时涉及了几个实例?"那么答案是两个,一个是字符串字面量"abc"所对应的,驻留在全局共享的字符串常量池中的实例,另一个是通过new String(String)创建并初始化的,内容与abc相同的实例。

首先必须弄清楚创建对象的含义。创建是什么时候创建的?这段代码在运行期间会创建2个对象么?毫无疑问不可能,用javap -c反编译即可得到JVM执行的字节码内容:

006ozJEaly1ghjghl4ppwj30m70e5q7p.jpg

很显然,new只调用了一次,也就是说只创建了一个对象。

而这道题目让人混淆的地方就是这里,这段代码在运行期间确实只创建了一个对象,即在堆上创建了"abc"对象。而为什么大家都在说是2个对象呢,这里面要澄清一个概念 该段代码执行过程和类的加载过程是有区别的。在类加载的过程中,确实在运行时常量池中创建了一个"abc"对象,而在代码执行过程中确实只创建了一个String对象。

因此,这个问题如果换成 String str = new String("abc")涉及到几个String对象?合理的解释是2个。

posted @ 2021-03-13 16:23  皆守  阅读(77)  评论(0)    收藏  举报