JAVA面试题 StringBuffer和StringBuilder的区别,从源码角度分析?

15 篇文章 0 订阅
10 篇文章 0 订阅

面试官Q1:请问StringBuffer和StringBuilder有什么区别?

这是一个老生常谈的话题,笔者前几年每次面试都会被问到,作为基础面试题,被问到的概率百分之八九十。下面我们从面试需要答到的几个知识点来总结一下两者的区别有哪些?

  • 继承关系?

  • 如何实现的扩容?

  • 线程安全性?

继承关系

从源码上看看类StringBuffer和StringBuilder的继承结构:

 

从结构图上可以直到,StringBuffer和StringBuiler都继承自AbstractStringBuilder类

 

如何实现扩容

StringBuffer和StringBuiler的扩容的机制在抽象类AbstractStringBuilder中实现,当发现长度不够的时候(默认长度是16),会自动进行扩容工作,扩展为原数组长度的2倍加2,创建一个新的数组,并将数组的数据复制到新数组。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

public void ensureCapacity(int minimumCapacity) {

    if (minimumCapacity > 0)

        ensureCapacityInternal(minimumCapacity);

}

 

/**

* 确保value字符数组不会越界.重新new一个数组,引用指向value

*/   

private void ensureCapacityInternal(int minimumCapacity) {

    // overflow-conscious code

    if (minimumCapacity - value.length > 0) {

        value = Arrays.copyOf(value,

                newCapacity(minimumCapacity));

    }

}

 

/**

* 扩容:将长度扩展到之前大小的2倍+2

*/   

private int newCapacity(int minCapacity) {

    // overflow-conscious code   扩大2倍+2

    //这里可能会溢出,溢出后是负数哈,注意

    int newCapacity = (value.length << 1) + 2;

    if (newCapacity - minCapacity < 0) {

        newCapacity = minCapacity;

    }

    //MAX_ARRAY_SIZE的值是Integer.MAX_VALUE - 8,先判断一下预期容量(newCapacity)是否在0<x<MAX_ARRAY_SIZE之间,在这区间内就直接将数值返回,不在这区间就去判断一下是否溢出

    return (newCapacity <= 0 || MAX_ARRAY_SIZE - newCapacity < 0)

        ? hugeCapacity(minCapacity)

        : newCapacity;

}

 

/**

* 判断大小,是否溢出

*/

private int hugeCapacity(int minCapacity) {

    if (Integer.MAX_VALUE - minCapacity < 0) { // overflow

        throw new OutOfMemoryError();

    }

    return (minCapacity > MAX_ARRAY_SIZE)

        ? minCapacity : MAX_ARRAY_SIZE;

}

 

线程安全性

我们先来看看StringBuffer的相关方法:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

@Override

public synchronized StringBuffer append(long lng) {

    toStringCache = null;

    super.append(lng);

    return this;

}

 

/**

 * @throws StringIndexOutOfBoundsException {@inheritDoc}

 * @since      1.2

 */

@Override

public synchronized StringBuffer replace(int start, int end, String str) {

    toStringCache = null;

    super.replace(start, end, str);

    return this;

}

 

/**

 * @throws StringIndexOutOfBoundsException {@inheritDoc}

 * @since      1.2

 */

@Override

public synchronized String substring(int start) {

    return substring(start, count);

}

 

@Override

public synchronized String toString() {

    if (toStringCache == null) {

        toStringCache = Arrays.copyOfRange(value, 0, count);

    }

    return new String(toStringCache, true);

}

从上面的源码中我们看到几乎都是所有方法都加了synchronized,几乎都是调用的父类的方法.,用synchronized关键字修饰意味着什么?加锁,资源同步串行化处理,所以是线程安全的。

 

我们再来看看StringBuilder的相关源码:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

@Override

public StringBuilder append(double d) {

    super.append(d);

    return this;

}

 

/**

 * @since 1.5

 */

@Override

public StringBuilder appendCodePoint(int codePoint) {

    super.appendCodePoint(codePoint);

    return this;

}

 

/**

 * @throws StringIndexOutOfBoundsException {@inheritDoc}

 */

@Override

public StringBuilder delete(int start, int end) {

    super.delete(start, end);

    return this;

}

StringBuilder的源码里面,基本上所有方法都没有用synchronized关键字修饰,当多线程访问时,就会出现线程安全性问题。

 

为了证明StringBuffer线程安全,StringBuilder线程不安全,我们通过一段代码进行验证:

测试思想

  • 分别用1000个线程写StringBuffer和StringBuilder,

  • 使用CountDownLatch保证在各自1000个线程执行完之后才打印StringBuffer和StringBuilder长度,

  • 观察结果。

测试代码

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

import java.util.concurrent.CountDownLatch;

 

public class TestStringBuilderAndStringBuffer {

    public static void main(String[] args) {

        //证明StringBuffer线程安全,StringBuilder线程不安全

        StringBuffer stringBuffer = new StringBuffer();

        StringBuilder stringBuilder = new StringBuilder();

        CountDownLatch latch1 = new CountDownLatch(1000);

        CountDownLatch latch2 = new CountDownLatch(1000);

        for (int i = 0; i < 1000; i++) {

            new Thread(new Runnable() {

                @Override

                public void run() {

                    try {

                        stringBuilder.append(1);

                    catch (Exception e) {

                        e.printStackTrace();

                    finally {

                        latch1.countDown();

                    }

                }

            }).start();

        }

        for (int i = 0; i < 1000; i++) {

            new Thread(new Runnable() {

                @Override

                public void run() {

                    try {

                        stringBuffer.append(1);

                    catch (Exception e) {

                        e.printStackTrace();

                    finally {

                        latch2.countDown();

                    }

 

                }

            }).start();

        }

        try {

            latch1.await();

            System.out.println(stringBuilder.length());

            latch2.await();

            System.out.println(stringBuffer.length());

        catch (InterruptedException e) {

            e.printStackTrace();

        }

    }

}

测试结果

  • StringBuffer不论运行多少次都是1000长度。

  • StringBuilder绝大多数情况长度都会小于1000。

  • StringBuffer线程安全,StringBuilder线程不安全得到证明。

总结一下

  • StringBuffer和StringBuilder都继承自抽象类AbstractStringBuilder。

  • 存储数据的字符数组也没有被final修饰,说明值可以改变,且构造出来的字符串还有空余位置拼接字符串,但是拼接下去肯定也有不够用的时候,这时候它们内部都提供了一个自动扩容机制,当发现长度不够的时候(默认长度是16),会自动进行扩容工作,扩展为原数组长度的2倍加2,创建一个新的数组,并将数组的数据复制到新数组,所以对于拼接字符串效率要比String要高。自动扩容机制是在抽象类中实现的。

  • 线程安全性:StringBuffer效率低,线程安全,因为StringBuffer中很多方法都被 synchronized 修饰了,多线程访问时,线程安全,但是效率低下,因为它有加锁和释放锁的过程。StringBuilder效率高,但是线程是不安全的。

 

    QQ交流群: 点击 有任何问题可以加群交流   

    Java最新一线架构技术讲解请:点击 内容实用 讲解简单易懂

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值