© 版权声明:本文为博主原创文章,转载请注明出处

String、StringBuffer、StringBuilder的区别

1.String是一个常量,其对象一旦创建完毕就无法改变,当使用“+”拼接字符串时,会生成新的String对象,而不是向原有的String对象追加内容。

 StringBuffer和StringBuilder都是变量,追加内容时不会生成新的对象。

2.String初始化的时候是最快的,StringBuilder其次,StringBuffer最慢

 但是修改内容的时候StringBuilder是最快的,StringBuffer其次,String最慢

3.String是常量,因此线程安全。

 StringBuffer是线程安全的,StringBuilder是非线程安全的

验证

1.String是常量,改变值会生成新的对象;StringBuffer和StringBuilder是变量,改变值不会生成新的对象

 /**
* 内存比较
*/
private static void memery() { String str1 = "abc";
System.out.println("String first memery address is " + str1.hashCode());
str1 += "efg";
System.out.println("String second memery address is " + str1.hashCode()); StringBuilder sb1 = new StringBuilder("abc");
System.out.println("StringBuilder first memery address is " + sb1.hashCode());
sb1.append("efg");
System.out.println("StringBuilder second memery address is " + sb1.hashCode()); StringBuffer sb2 = new StringBuffer("abc");
System.out.println("StringBuffer first memery address is " + sb2.hashCode());
sb2.append("efg");
System.out.println("StringBuffer second memery address is " + sb2.hashCode()); }

memery

结果:

2.初始化速度:String>StringBuilder>StringBuffer

 /**
* 初始化性能比较
*/
private static void initial() { long time1 = System.nanoTime();
String str1 = "abc";
long time2 = System.nanoTime();
System.out.println("String time: " + (time2 - time1) + "ns"); long time3 = System.nanoTime();
StringBuffer sb1 = new StringBuffer("abc");
long time4 = System.nanoTime();
System.out.println("StringBuffer time: " + (time4 - time3) + "ns"); long time5 = System.nanoTime();
StringBuilder sb2 = new StringBuilder("abc");
long time6 = System.nanoTime();
System.out.println("StringBuilder time: " + (time6 - time5) + "ns"); }

initial

结果:

3.执行速度(即改变值的时候):StringBuilder>StringBuffer>String

 /**
* 性能比较
*/
private static void performance() { long time1 = System.nanoTime();
String str1 = "";
for (int i = 0; i < 10; i++) {
str1 += i;
}
long time2 = System.nanoTime();
System.out.println("the time required for String to execute 1000 times: " + (time2 - time1) + "ns"); long time3 = System.nanoTime();
StringBuffer sb1 = new StringBuffer();
for (int i = 0; i < 10; i++) {
sb1.append(i);
}
long time4 = System.nanoTime();
System.out.println("the time required for StringBuffer to execute 1000 times: " + (time4 - time3) + "ns"); long time5 = System.nanoTime();
StringBuilder sb2 = new StringBuilder();
for (int i = 0; i < 10; i++) {
sb2.append(i);
}
long time6 = System.nanoTime();
System.out.println("the time required for StringBuilder to execute 1000 times: " + (time6 - time5) + "ns"); }

performance

结果:

4.特殊例子:当执行String str = "a" + "b" + "c"时,String的速度是最快的,因为等价于String = "abc";上面讲过,初始化的时候String是最快的

 /**
* 特殊例子
*/
private static void special() { long time1 = System.nanoTime();
String str1 = "abc" + "def" + "ghi" + "jk";
long time2 = System.nanoTime();
System.out.println("String time: " + (time2 - time1) + "ns"); long time3 = System.nanoTime();
StringBuffer sb1 = new StringBuffer("abc").append("def").append("ghi").append("jk");
long time4 = System.nanoTime();
System.out.println("StringBuffer time: " + (time4 - time3) + "ns"); long time5 = System.nanoTime();
StringBuilder sb2 = new StringBuilder("abc").append("def").append("ghi").append("jk");
long time6 = System.nanoTime();
System.out.println("StringBuilder time: " + (time6 - time5) + "ns"); }

special

结果:

5.StringBuffer是线程安全的,StringBuilder是非线程安全的

 /**
* 线程安全性验证
*/
public static void thread() { final StringBuffer sb1 = new StringBuffer();
final StringBuilder sb2 = new StringBuilder();
final CountDownLatch latch1 = new CountDownLatch(1000);
final CountDownLatch latch2 = new CountDownLatch(1000); for (int i = 0; i < 1000; i++) {
new Thread(new Runnable() {
public void run() {
try {
sb1.append("1");
} finally {
latch1.countDown();
} }
}).start();
}
for (int i = 0; i < 1000; i++) {
new Thread(new Runnable() {
public void run() {
try {
sb2.append("2");
} finally {
latch2.countDown();
}
}
}).start();
} try {
latch1.await();
System.out.println(sb1.length());
latch2.await();
System.out.println(sb2.length());
} catch (Exception e) {
e.printStackTrace();
} }

thread

结果:需多执行几次,StringBuffer长度全部为1000,StringBuilder大部分都是小于1000的

6.完整代码

 package org.java.base;

 import java.util.concurrent.CountDownLatch;

 /**
* String、StringBuffer、StringBuilder比较
*/
public class StringAndStringBufferAndStringBuilder { public static void main(String[] args) { //memery();
//initial();
//performance();
//special();
thread(); } /**
* 内存比较
*/
private static void memery() { String str1 = "abc";
System.out.println("String first memery address is " + str1.hashCode());
str1 += "efg";
System.out.println("String second memery address is " + str1.hashCode()); StringBuilder sb1 = new StringBuilder("abc");
System.out.println("StringBuilder first memery address is " + sb1.hashCode());
sb1.append("efg");
System.out.println("StringBuilder second memery address is " + sb1.hashCode()); StringBuffer sb2 = new StringBuffer("abc");
System.out.println("StringBuffer first memery address is " + sb2.hashCode());
sb2.append("efg");
System.out.println("StringBuffer second memery address is " + sb2.hashCode()); } /**
* 初始化性能比较
*/
private static void initial() { long time1 = System.nanoTime();
String str1 = "abc";
long time2 = System.nanoTime();
System.out.println("String time: " + (time2 - time1) + "ns"); long time3 = System.nanoTime();
StringBuffer sb1 = new StringBuffer("abc");
long time4 = System.nanoTime();
System.out.println("StringBuffer time: " + (time4 - time3) + "ns"); long time5 = System.nanoTime();
StringBuilder sb2 = new StringBuilder("abc");
long time6 = System.nanoTime();
System.out.println("StringBuilder time: " + (time6 - time5) + "ns"); } /**
* 性能比较
*/
private static void performance() { long time1 = System.nanoTime();
String str1 = "";
for (int i = 0; i < 10; i++) {
str1 += i;
}
long time2 = System.nanoTime();
System.out.println("the time required for String to execute 1000 times: " + (time2 - time1) + "ns"); long time3 = System.nanoTime();
StringBuffer sb1 = new StringBuffer();
for (int i = 0; i < 10; i++) {
sb1.append(i);
}
long time4 = System.nanoTime();
System.out.println("the time required for StringBuffer to execute 1000 times: " + (time4 - time3) + "ns"); long time5 = System.nanoTime();
StringBuilder sb2 = new StringBuilder();
for (int i = 0; i < 10; i++) {
sb2.append(i);
}
long time6 = System.nanoTime();
System.out.println("the time required for StringBuilder to execute 1000 times: " + (time6 - time5) + "ns"); } /**
* 特殊例子
*/
private static void special() { long time1 = System.nanoTime();
String str1 = "abc" + "def" + "ghi" + "jk";
long time2 = System.nanoTime();
System.out.println("String time: " + (time2 - time1) + "ns"); long time3 = System.nanoTime();
StringBuffer sb1 = new StringBuffer("abc").append("def").append("ghi").append("jk");
long time4 = System.nanoTime();
System.out.println("StringBuffer time: " + (time4 - time3) + "ns"); long time5 = System.nanoTime();
StringBuilder sb2 = new StringBuilder("abc").append("def").append("ghi").append("jk");
long time6 = System.nanoTime();
System.out.println("StringBuilder time: " + (time6 - time5) + "ns"); } /**
* 线程安全性验证
*/
public static void thread() { final StringBuffer sb1 = new StringBuffer();
final StringBuilder sb2 = new StringBuilder();
final CountDownLatch latch1 = new CountDownLatch(1000);
final CountDownLatch latch2 = new CountDownLatch(1000); for (int i = 0; i < 1000; i++) {
new Thread(new Runnable() {
public void run() {
try {
sb1.append("1");
} finally {
latch1.countDown();
} }
}).start();
}
for (int i = 0; i < 1000; i++) {
new Thread(new Runnable() {
public void run() {
try {
sb2.append("2");
} finally {
latch2.countDown();
}
}
}).start();
} try {
latch1.await();
System.out.println(sb1.length());
latch2.await();
System.out.println(sb2.length());
} catch (Exception e) {
e.printStackTrace();
} } }

StringAndStringBufferAndStringBuilder

String、StringBuffer、StringBuilder区别并验证的更多相关文章

  1. 转:String StringBuffer StringBuilder区别

    转自:http://www.iteye.com/topic/522167 作者:每次上网冲杯Java时,都能看到关于String无休无止的争论.还是觉得有必要让这个讨厌又很可爱的String美眉,赤裸 ...

  2. String&StringBuffer&StringBuilder区别

    String  String类是final类故不可以继承,也就意味着String引用的字符串内容是不能被修改.String有两种实例化方式:    (1)直接赋值(例中,String str = &q ...

  3. String,StringBuffer,StringBuilder区别

    String 字符串常量StringBuffer 字符串变量(线程安全)StringBuilder 字符串变量(非线程安全) 简要的说, String 类型和 StringBuffer 类型的主要性能 ...

  4. Java中String/StringBuffer/StringBuilder区别(转)

    1.三者在执行速度方面的比较:StringBuilder >  StringBuffer  >  String 2.String <(StringBuffer,StringBuild ...

  5. String StringBuffer StringBuilder区别与联系

    java.lang.String.java.lang.StringBuffer.java.lang.StringBuilder都是字符串类型,是Java中用于处理字符串常用的三个类.它们主要有以下区别 ...

  6. Java学习之String StringBuffer StringBuilder区别

    1.String:对象长度是不可变的,是线程安全. 2.StringBuffer:对象长度是可变的,是线程安全. 3.StringBuilder:对象长度是可变,不是线程安全.

  7. String,StringBuffer,StringBuilder区别(笔记)

    String类被final修饰,创建的对象为不可变对象,属于字符串常量. 而StringBuffer与StringBuilder创建的属于字符串常量. StringBuffer的方法大多用了Synch ...

  8. java中string , StringBuffer , StringBuilder 区别

    1.String String变量的值不能改变,如果要改变String变量的值,虚拟机首先会遍历方法区中的字符串常量,如果存在需要的值,则虚拟机直接把此常量值的地址分配给String变量,如果不存在这 ...

  9. String StringBuffer stringbuilder 区别

    韩梦飞沙  韩亚飞  313134555@qq.com  yue31313  han_meng_fei_sha 字符串类 ,长度不可变. 字符串缓存器类, 长度可变, 线程安全, 字符串构造器类,长度 ...

  10. 【Java基础】String StringBuffer StringBuilder

    String String是不可变的 我们都知道String不是基本数据类型,而是一个对象,并且是final类型的,不可变的.(public final class String) 查看以下代码: S ...

随机推荐

  1. MSSQL—字符串分离(Split函数)

    前面提到了记录合并,有了合并需求肯定也会有分离需求,说到字符串分离,大家肯定会想到SPLIT函数,这个在.NET,Java和JS中都有函数,很可惜在SQL SERVER中没有,我们只能自己来写这么一个 ...

  2. ZOJ 4009 And Another Data Structure Problem(ZOJ Monthly, March 2018 Problem F,发现循环节 + 线段树 + 永久标记)

    题目链接  ZOJ Monthly, March 2018 Problem F 题意很明确 这个模数很奇妙,在$[0, mod)$的所有数满足任意一个数立方$48$次对$mod$取模之后会回到本身. ...

  3. HDU6380 2018 “百度之星”程序设计大赛 - 初赛(B) A-degree (无环图=树)

    原题地址 degree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Tot ...

  4. 监控SQL Server正在执行的SQL语句和死锁情况

    原文:监控SQL Server正在执行的SQL语句和死锁情况 SELECT [Individual Query] = SUBSTRING(qt.TEXT, er.statement_start_off ...

  5. linux之网络配置相关

    ubuntu的网络配置文件在 /etc/network/intrfaces; suse的网络配置在          /etc/sysconfig/network/下面,每个网卡一个配置文件. int ...

  6. hash算法散列算法

    Hash,一般翻译做“散列”,也有直接音译为“哈希”的,就是把任意长度的输入(又叫做预映射, pre-image),通过散列算法,变换成固定长度的输出,该输出就是散列值.这种转换是一种压缩映射,也就是 ...

  7. BeanUtils——JavaBean相互转换及字典翻译

    JavaBean相互转换 在升级公司架构过程中,发现有大量Entity与DTO相互转换的问题,并且其中还伴随DTO中的数据字典翻译,所以特意写个工具类,主要利用spring提供的BeanUtils工具 ...

  8. LCIS线段树(区间更新)

    首先线段树每一个节点包含:[b,e],lmax,rmax,max;其中lmax表示从左端点开始连续的最长的增序列长度,rmax表示从e端点开始向左连续的最长下降序列长度,max表示当前区间的连续递增的 ...

  9. 设计模式之迭代器模式(PHP实现)

    github地址:https://github.com/ZQCard/design_pattern/** * 迭代器模式(Iterator Pattern)是 Java 和 .Net 编程环境中非常常 ...

  10. Java:网络编程之应用实例

    1.聊天 编写一个聊天程序,有收数据的部分和发数据的部分. 这两部分需要同时执行,那么就需要多线程技术. 一个线程控制发送. 一个线程控制接收. 因为收和发动作是不一致的,所以要定义两个run方法,而 ...