1. String是不可变类,改变String变量中的值,相当于开辟了新的空间存放新的string变量

2. StringBuffer 可变的类,可以通过append方法改变变量的值,且StringBuffer是线程安全的,它的很多方法都是同步方法,支持并发操作,适用于多线程    

3. StringBuilder 可变的类,但是线程不安全的,用于单线程中性能高于StringBuffer

4. HashTable 线程安全的,HashMap线程不安全的

为什么StringBuffer是同步的?

因为很多方法都是synchronized 。。。

 /*
* @(#)StringBuffer.java 1.101 05/11/17
*
* Copyright 2006 Sun Microsystems, Inc. All rights reserved.
* SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*/ package java.lang; /**
* A thread-safe, mutable sequence of characters.
* A string buffer is like a {@link String}, but can be modified. At any
* point in time it contains some particular sequence of characters, but
* the length and content of the sequence can be changed through certain
* method calls.
* <p>
* String buffers are safe for use by multiple threads. The methods
* are synchronized where necessary so that all the operations on any
* particular instance behave as if they occur in some serial order
* that is consistent with the order of the method calls made by each of
* the individual threads involved.
* <p>
* The principal operations on a <code>StringBuffer</code> are the
* <code>append</code> and <code>insert</code> methods, which are
* overloaded so as to accept data of any type. Each effectively
* converts a given datum to a string and then appends or inserts the
* characters of that string to the string buffer. The
* <code>append</code> method always adds these characters at the end
* of the buffer; the <code>insert</code> method adds the characters at
* a specified point.
* <p>
* For example, if <code>z</code> refers to a string buffer object
* whose current contents are "<code>start</code>", then
* the method call <code>z.append("le")</code> would cause the string
* buffer to contain "<code>startle</code>", whereas
* <code>z.insert(4, "le")</code> would alter the string buffer to
* contain "<code>starlet</code>".
* <p>
* In general, if sb refers to an instance of a <code>StringBuffer</code>,
* then <code>sb.append(x)</code> has the same effect as
* <code>sb.insert(sb.length(), x)</code>.
* <p>
* Whenever an operation occurs involving a source sequence (such as
* appending or inserting from a source sequence) this class synchronizes
* only on the string buffer performing the operation, not on the source.
* <p>
* Every string buffer has a capacity. As long as the length of the
* character sequence contained in the string buffer does not exceed
* the capacity, it is not necessary to allocate a new internal
* buffer array. If the internal buffer overflows, it is
* automatically made larger.
*
* As of release JDK 5, this class has been supplemented with an equivalent
* class designed for use by a single thread, {@link StringBuilder}. The
* <tt>StringBuilder</tt> class should generally be used in preference to
* this one, as it supports all of the same operations but it is faster, as
* it performs no synchronization.
*
* @author Arthur van Hoff
* @version 1.101, 11/17/05
* @see java.lang.StringBuilder
* @see java.lang.String
* @since JDK1.0
*/
public final class StringBuffer
extends AbstractStringBuilder
implements java.io.Serializable, CharSequence
{ /** use serialVersionUID from JDK 1.0.2 for interoperability */
static final long serialVersionUID = 3388685877147921107L; /**
* Constructs a string buffer with no characters in it and an
* initial capacity of 16 characters.
*/
public StringBuffer() {
super(16);
} /**
* Constructs a string buffer with no characters in it and
* the specified initial capacity.
*
* @param capacity the initial capacity.
* @exception NegativeArraySizeException if the <code>capacity</code>
* argument is less than <code>0</code>.
*/
public StringBuffer(int capacity) {
super(capacity);
} /**
* Constructs a string buffer initialized to the contents of the
* specified string. The initial capacity of the string buffer is
* <code>16</code> plus the length of the string argument.
*
* @param str the initial contents of the buffer.
* @exception NullPointerException if <code>str</code> is <code>null</code>
*/
public StringBuffer(String str) {
super(str.length() + 16);
append(str);
} /**
* Constructs a string buffer that contains the same characters
* as the specified <code>CharSequence</code>. The initial capacity of
* the string buffer is <code>16</code> plus the length of the
* <code>CharSequence</code> argument.
* <p>
* If the length of the specified <code>CharSequence</code> is
* less than or equal to zero, then an empty buffer of capacity
* <code>16</code> is returned.
*
* @param seq the sequence to copy.
* @exception NullPointerException if <code>seq</code> is <code>null</code>
* @since 1.5
*/
public StringBuffer(CharSequence seq) {
this(seq.length() + 16);
append(seq);
} public synchronized int length() {
return count;
} public synchronized int capacity() {
return value.length;
} ...

既然StringBuffer是线程同步的,那么代价就是损失性能,摘自一个测试http://blog.sina.com.cn/s/blog_5749ead90100b7lq.html

public class TestBufferAndBuilder {
public static void main(String[] args) { String randoms[] = { String.valueOf(Math.random()), String.valueOf(Math.random()), String.valueOf(Math.random()), String.valueOf(Math.random()),
String.valueOf(Math.random()), String.valueOf(Math.random()), String.valueOf(Math.random()), String.valueOf(Math.random()), String.valueOf(Math.random()),
String.valueOf(Math.random()) }; System.gc();
long d = System.currentTimeMillis();
for (int i = 0; i < 1000000; i++) {
StringBuffer buffer = new StringBuffer();
buffer.append(randoms[0]).append(randoms[1]).append(randoms[2]).append(randoms[3]).append(randoms[4]).append(randoms[5]).append(randoms[6]).append(randoms[7]).append(
randoms[8]).append(randoms[9]);
}
System.err.println(System.currentTimeMillis() - d);
System.gc(); d = System.currentTimeMillis();
for (int i = 0; i < 1000000; i++) {
StringBuilder builder = new StringBuilder();
builder.append(randoms[0]).append(randoms[1]).append(randoms[2]).append(randoms[3]).append(randoms[4]).append(randoms[5]).append(randoms[6]).append(randoms[7]).append(
randoms[8]).append(randoms[9]);
}
System.err.println(System.currentTimeMillis() - d); System.gc();
d = System.currentTimeMillis();
for (int i = 0; i < 1000000; i++) {
StringBuffer buffer = new StringBuffer(200);
buffer.append(randoms[0]).append(randoms[1]).append(randoms[2]).append(randoms[3]).append(randoms[4]).append(randoms[5]).append(randoms[6]).append(randoms[7]).append(
randoms[8]).append(randoms[9]);
}
System.err.println(System.currentTimeMillis() - d);
System.gc(); d = System.currentTimeMillis();
for (int i = 0; i < 1000000; i++) {
StringBuilder builder = new StringBuilder(200);
builder.append(randoms[0]).append(randoms[1]).append(randoms[2]).append(randoms[3]).append(randoms[4]).append(randoms[5]).append(randoms[6]).append(randoms[7]).append(
randoms[8]).append(randoms[9]);
}
System.err.println(System.currentTimeMillis() - d); }
}

输出结果:

3188
    3657
    1672
    1859

结果如上,基本上要慢10-20%

StringBuffer和StringBuilder区别?的更多相关文章

  1. String、StringBuffer、StringBuilder区别

    String.StringBuffer.StringBuilder区别 StringBuffer.StringBuilder和String一样,也用来代表字符串.String类是不可变类,任何对Str ...

  2. String、StringBuffer和StringBuilder区别

    String.StringBuffer和StringBuilder区别 1.长度是否可变 String 是被 final 修饰的,他的长度是不可变的,就算调用 String 的concat 方法,那也 ...

  3. 【37】String,StringBuffer,StringBuilder区别和概念

    基本的概念: 查看 API 会发现,String.StringBuffer.StringBuilder 都实现了 CharSequence 接口,内部都是用一个char数组实现,虽然它们都与字符串相关 ...

  4. String、StringBuffer、StringBuilder区别并验证

    © 版权声明:本文为博主原创文章,转载请注明出处 String.StringBuffer.StringBuilder的区别 1.String是一个常量,其对象一旦创建完毕就无法改变,当使用“+”拼接字 ...

  5. Java中的String、StringBuffer、StringBuilder区别以及Java之StringUtils的用法

    1.String.StringBuffer.StringBuilder的区别 String是Java中基础类型,是immutable类(不可变)的典型实现,利用string进行拼接是会产生过多无用对象 ...

  6. Java中String、StringBuffer、StringBuilder区别与理解

    一.先比较String.StringBuffer.StringBuilder变量的HashCode值 使用System.out.println(obj.hashcode())输出的时对象的哈希码, 而 ...

  7. String、StringBuffer和StringBuilder区别及性能分析

    1.性能比较:StringBuilder >  StringBuffer  >  String 2.String <(StringBuffer,StringBuilder)的原因 S ...

  8. String StringBuffer和StringBuilder区别及性能

    结论: (1)如果要操作少量的数据用 String: (2)多线程操作字符串缓冲区下操作大量数据 StringBuffer: (3)单线程操作字符串缓冲区下操作大量数据 StringBuilder(推 ...

  9. stringbuffer 和 stringbuilder区别

    stringbuffer  和  stringbuilder速度                 小于         线程安全           线程非安全 单线程操作大量数据用stringbui ...

  10. String、StringBuffer与StringBuilder区别

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

随机推荐

  1. 商品录入功能v1.0【持续优化中...】

    # 录入商品 def goods_record(): print("欢迎使用铜锣辉的购物商城[商品管理][录入商品]".center(30, "*")) whi ...

  2. 读经典——《CLR via C#》(Jeffrey Richter著) 笔记_CLR

    1.CLR简介 全称:Common Language Runtime(公共语言进行时) 属性:一种托管模块 使用对象:面向CLR的所有语言(C#.Basic.IL...) 核心功能:内存管理.程序集加 ...

  3. git 工作中常用命令

    git 命令: git  init  : 初始化 git  add .  :添加所有文件 git  status  :查看状态 若果是第一次会提示你输入你的 邮箱 和姓名: git  commit  ...

  4. python自动化day2-列表、字典、集合

    一.数据类型 1.什么是数据? x=10,10是我们要存储的数据 2.为何数据要分不同的类型 数据是用来表示状态的,不同的状态就应该用不同的类型的数据去表示 3 数据类型 数字(整形,长整形,浮点型, ...

  5. Vue 中怎么发起请求(一)

    1.vue 支持开发者引入 jquery 使用 $.ajax() 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 1.首先,在 package.json 中添加 j ...

  6. vue 的watch用法

    参考转自https://www.imooc.com/article/details/id/28187 类型:{ [key: string]: string | Function | Object | ...

  7. webpack安装与配置(window)

    最近几天也是刚刚学习webpack工具,所以就要从安装开始我的学习的第一步.在网上搜索了找到webpack官网,在下载webpack就要先安装nodejs,在nodejs里用集成的npm下载webpa ...

  8. CharacterController控制的物体移动

    CharacterController控制的物体移动: public class playerMove  : MonoBehaviour { public float Speed; private C ...

  9. vue-cli构建项目在index.html中使用静态文件

    在vue-cli构建的项目中,且使用在移动端,我们希望每一个页面加载时都可以使用flexible.js来适配手机. 那么这个flexible.js被import到每一个组件中就不合适了. 好的方法是直 ...

  10. opensuse13.2安装 sass和compass

    首先要先安装ruby 和 gem如果使用sudo zypper install ruby 安装后 当安装sass时会报错 /System/Library/Frameworks/Ruby.framewo ...