Java的String&StringBuilder&StringBuffer

大三暑假实习的技术分享

一、String类源码分析

1. 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中,被final关键字修饰的类不允许被继承,且该类的所有成员方法都默认为final方法。final方法不可以被(该类的子类)覆盖(override)。
(2)String通过私有成员char value[ ]数组保存字符串。

2. 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;

从以上的substring、concat、replace三个方法可以看出,返回的字符串都是一个重新new的一个String对象,也就是说这些操作并不是在当前的字符串上进行修改的。总结为下面这句话:

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

二、String、StringBuilder、StringBuffer的区别

1.执行速度方面

首先来看下面这段代码:

public class Main {

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

反编译的字节码如下:

在上图字节码中,从第8行到第35行是源代码中循环语句string+= “hello”的执行过程,每次执行都会new一个StringBuilder对象,然后进行append操作,最后通过toString方法返回String对象:

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

StringBuilder类的toString方法如下:

public String toString() {  
// Create a copy, don't share the array
return new String(value, 0, count);
}

也需要new一个String的对象,因此循环1000次将造成巨大的内存浪费,且执行速度很慢。

再看以下代码:

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

反编译字节码文件得到:


  

字节码的13行到27行是源代码循环的执行过程,其中并没有任何new的操作,即append操作都是在原有对象的基础上进行的,因此循环1000次占用的内存资源要小得多,执行速度也更快。

三者的执行效率StringBuilder>StringBuffer>String,当然这个是相对的,也有例外情况,比如以下代码:

String str="abc"+"de";
StringBuilder stringBuilder=new StringBuilder().append("abc").append("de");
System.out.println(str);
System.out.println(stringBuilder.toString());

String的速度比StringBuilder的反应速度快很多,因为对于直接相加字符串,在编译器便确定了它的值,也就是说”abc”+”de”的字符串相加,在编译期间便被优化成了”abcde”。对于间接相加(即包含字符串引用),形如s1+s2+s3,效率要比直接相加低,因为在编译器不会对引用变量进行优化。

2.线程安全方面

StringBuilder和StringBuffer类所拥有的成员属性和成员方法基本相同,区别是StringBuffer类的成员方法前面多了一个关键字synchronized,因此StringBuffer是线程安全的,StringBuilder是非线程安全的。下面摘了两段代码分别来自二者的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;
}

3.适用情况

String:适用于少量字符串操作的情况
StringBuilder:适用于单线程下对大量字符串操作
StringBuffer:适用于多线程下对大量字符串操作

文章作者: Moon Lou
文章链接: https://loumoon.github.io/2018/07/21/Java的String&StringBuilder&StringBuffer/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 Moon's Blog