【JAVA基础】String、StringBuilder和StringBuffer的区别——巨详细

【JAVA基础】String、StringBuilder和StringBuffer的区别------巨详细

先给答案

String是不可变的,StringBuilderStringBuffer是可变的。而StringBuffer是线程安全的,而StringBuilder是非线程安全的。

源码

先看看jdk1.8中关于String、StringBuilder和StringBuffer部分的源码,我们看某个类或者某个属性是否不可变首先要看修饰类的关键字是什么,final表示不可改变也不可继承。

String

arduino 复制代码
public final class String
    implements java.io.Serializable, Comparable<String>, CharSequence {
    // String内部使用char数组来存储数据
    private final char value[];
​
    // ...
}

源码中String类和String类的值都采用final修饰,因此String类型是不可变的。

StringBuilder

java 复制代码
public final class StringBuilder
    extends AbstractStringBuilder
    implements java.io.Serializable, CharSequence {
    
    // 数组最大长度
    private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8
​
    // ... 其他方法
​
    // 示例方法:添加字符串
    public StringBuilder append(String str) {
        // 这里的super指向AbstractStringBuilder类
        super.append(str);
        return this;
    }
    
    // ... 其他方法继续
}
​
abstract class AbstractStringBuilder {
    char[] value;
    int count;
​
    // 实际扩展数组和添加内容在这个类中实现
    public void ensureCapacity(int minimumCapacity) {
        if (minimumCapacity > 0)
            ensureCapacityInternal(minimumCapacity);
    }
    
    private void ensureCapacityInternal(int minimumCapacity) {
        // overflow-conscious code
        if (minimumCapacity - value.length > 0) {
            value = Arrays.copyOf(value,
                    newCapacity(minimumCapacity));
        }
    }
    
    private int newCapacity(int minCapacity) {
        // overflow-conscious code
        int newCapacity = (value.length << 1) + 2;
        if (newCapacity - minCapacity < 0) {
            newCapacity = minCapacity;
        }
        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;
    }
​
    public AbstractStringBuilder append(String str) {
        if (str == null) {
            // ... 处理null字符串的情况
        }
        int len = str.length();
        ensureCapacityInternal(count + len);
        str.getChars(0, len, value, count);
        count += len;
        return this;
    }
    
    // ... 其他方法
}

实际上在append的过程中调用的是抽象类AbstractStringBuilder中的append方法,从这里可以看出append方法首先根据这个字符串的长度对当前的字符数组进行扩容,可以看到StringBuilder存储字符串类型用的也是char[] value,但是这里的修饰符是缺省,因此可以对其进行扩容,即可变。

StringBuffer

StringBufferStringBuilder的差异不大,唯一的区别就是加上了关键字synchronized

java 复制代码
 public final class StringBuffer
    extends AbstractStringBuilder
    implements Serializable, CharSequence
{
    ...
    private transient char[] toStringCache;
    ...
    @Override
    public synchronized StringBuffer append(String str) {
        toStringCache = null;
        super.append(str);
        return this;
    }
    ...
}

String的"+"操作

反编译

ini 复制代码
long t1 = System.currentTimeMillis();
String str = "hollis";
for (int i = 0; i < 50000; i++) {
    String s = String.valueOf(i);
    str += s;
}
long t2 = System.currentTimeMillis();
System.out.println("+ cost:" + (t2 - t1));
ini 复制代码
long t1 = System.currentTimeMillis();
String str = "hollis";
for(int i = 0; i < 50000; i++)
{
    String s = String.valueOf(i);
    str = (new StringBuilder()).append(str).append(s).toString();
}
​
long t2 = System.currentTimeMillis();
System.out.println((new StringBuilder()).append("+ cost:").append(t2 - t1).toString());

测试demo

ini 复制代码
package org.example;
​
public class Main {
    public static void main(String[] args) {
        testStringAdd();
        testStringBuilderAdd();
    }
​
    static void testStringAdd() {
        Runtime runtime = Runtime.getRuntime();
​
        long usedMemoryBefore = runtime.totalMemory() - runtime.freeMemory();
        long startTime = System.currentTimeMillis();
​
        String result = "";
        for (int i = 0; i < 10000; i++) {
            result += "some text";
        }
​
        long endTime = System.currentTimeMillis();
        System.out.println("String concatenation with + operator took: " + (endTime - startTime) + " milliseconds");
        long usedMemoryAfter = runtime.totalMemory() - runtime.freeMemory();
        System.out.println("Memory used for String concatenation: " + (usedMemoryAfter - usedMemoryBefore) + " bytes");
    }
​
    static void testStringBuilderAdd() {
        Runtime runtime = Runtime.getRuntime();
​
        long usedMemoryBefore = runtime.totalMemory() - runtime.freeMemory();
        long startTime = System.currentTimeMillis();
​
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < 10000; i++) {
            builder.append("some text");
        }
​
        long endTime = System.currentTimeMillis();
        System.out.println("String concatenation with StringBuilder took: " + (endTime - startTime) + " milliseconds");
​
        long usedMemoryAfter = runtime.totalMemory() - runtime.freeMemory();
        System.out.println("Memory used for String concatenation: " + (usedMemoryAfter - usedMemoryBefore) + " bytes");
    }
}

从控制台上可以看到两者的性能差异十分明显。

vbnet 复制代码
String concatenation with + operator took: 669 milliseconds
Memory used for String concatenation: 619980944 bytes
String concatenation with StringBuilder took: 0 milliseconds
Memory used for String concatenation: 0 bytes

testStringAdd中的"+"部分反编译后,得到如下代码:

ini 复制代码
String result = "";
for (int i = 0; i < 10000; i++) {
    result = (new StringBuilder()).append("some text").toString();
}

这里可以看出来实际上"+"做的操作就是new StringBuilder()

使用场景

  • 如果字符串不需要修改,或者只是偶尔修改,使用String
  • 如果在单线程环境中需要频繁修改字符串,使用StringBuilder
  • 如果在多线程环境中需要频繁修改字符串,使用StringBuffer
相关推荐
MediaTea28 分钟前
Python 第三方库:Flask(轻量级 Web 框架)
开发语言·前端·后端·python·flask
q***72561 小时前
Spring Boot + Vue 全栈开发实战指南
vue.js·spring boot·后端
清晨细雨~1 小时前
SpringBoot整合EasyExcel实现Excel表头校验
spring boot·后端·excel
canonical_entropy1 小时前
API无缝升级方案:从推模式到拉模式的架构演进
后端·restful·graphql
摆烂工程师1 小时前
今天 Cloudflare 全球事故,连 GPT 和你的网站都一起“掉线”了
前端·后端·程序员
追逐时光者2 小时前
快速构建一个基础、现代化的 WinForm 管理系统
后端·.net
在人间负债^3 小时前
Rust 实战项目:TODO 管理器
开发语言·后端·rust
Moonbit4 小时前
入围名单公布|2025 MGPIC 决赛即将拉开帷幕!
后端·算法
爱吃烤鸡翅的酸菜鱼4 小时前
用【rust】实现命令行音乐播放器
开发语言·后端·rust
黛琳ghz4 小时前
用 Rust 从零构建高性能文件加密工具
开发语言·后端·rust