String / StringBuffer / StringBuilder 的区别与使用场景

Java的JDK中自带的3个字符串类 String / StringBuffer / StringBuilder
使用时应该分清楚各自的使用情景:


  

String

1:

  • The {@code String} class represents character strings. All
  • string literals in Java programs, such as {@code "abc"}, are
  • implemented as instances of this class.

String类代表了所有字符串 , 所有在Java中使用的字符串 , 例如 "abc" 就是String类的一个实例.

也就是说, String s = new String("abc"); 中的"abc"也是String
此行其实就是调用了 String类的有一个String的构造方法:

public String(String original) {
     //...
}

2:

  • Strings are constant; their values cannot be changed after they are created.

String是常量, 他的值是无法在创建后被改变的.

看个例子:

String s = "111";
System.out.println(s.hashCode());          //48657

s = s + "222";
System.out.println(s.hashCode());          //1449590337

最后的s操作后, 已经不是原来的s对象了. 这个例子可以看出String对象并不能对本身直接操作, 操作只会生成一个新的对象

String 就是一个基本的内置类 , 使用时应该避免频繁的操作对象的情况

:

StringBuffer

  • A thread-safe, mutable sequence of characters.
  • A string buffer is like a {@link String}, but can be modified

一个线程安全可变的字符串类, 它是String类的可修改版本

  • String buffers are safe for use by multiple threads. The methods
  • are synchronized where necessary

它是线程安全的, 类中的必要方法都使用来了(synchronized)关键字

StringBuffer类提供了append方法来连接一个字符串到对象

StringBuffer s = new StringBuffer("111");
System.out.println(s.hashCode());               //1956725890

s = s.append("222");
System.out.println(s.hashCode());               //1956725890

可以看出append操作之后, s仍然是之前的那个对象.

所以 StringBuffer是在频繁操作字符串时的选择 , 同时他使用了 synchronized 进行了线程保护 , 是线程安全的.

:

StringBuilder

  • A mutable sequence of characters. This class provides an API compatible
  • with {@code StringBuffer}, but with no guarantee of synchronization.

一个可修改的字符串序列, 这个类提供了与StringBuffer相似的API, 但是没有使用同步保护, 代表它不是线程安全的.

也就是说, 它是非线程安全的的可修改字符串类.
虽然没有使用同步保护, 但优点是它可以提供比StringBuffer更快的操作速度
在没有多线程且规模化操作字符串时为首选.

  

最后对比一下操作速度:

class Test {
    // 输出执行时间
    long startTime;
    void start() {
        startTime = System.nanoTime();
    }
    void end() {
        System.out.printf("执行时间: %d\n",System.nanoTime()-startTime);
    }

    // 测试反复修改字符串
    public void testString() {
        start();
        String s="";
        for(int i=0;i<1000;i++){
            s += "1";
        }
        end();
    }
    public void testStringBuffer() {
        start();
        StringBuffer sb = new StringBuffer();
        for(int i=0;i<1000;i++){
            sb.append("1");
        }
        end();
    }
    public  void testStringBuilder() {
        start();
        StringBuilder sb = new StringBuilder();
        for(int i=0;i<1000;i++){
            sb.append("1");
        }
        end();
    }
}

public class main {
    public static void main(String[] argv) {
        Test t = new Test();
        t.testString();
        t.testStringBuffer();
        t.testStringBuilder();
    }
}


/*
输出:  
执行时间: 1717460  
执行时间: 103881  
执行时间: 49142  
*/

转载于:https://my.oschina.net/tasker/blog/817420

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值