Java String, StringBuilder, StringBuffer

整理自stackoverflow:

Mutability Difference:

String is immutable, if you try to alter their values, another object gets created, whereas StringBuffer and StringBuilder are mutable so they can change their values.

Thread-Safety Difference:

The difference between StringBuffer and StringBuilder is that StringBufferis thread-safe. So when the application needs to be run only in a single thread then it is better to use StringBuilderStringBuilder is more efficient than StringBuffer.

Situations:

  • If your string is not going to change use a String class because a Stringobject is immutable.
  • If your string can change (example: lots of logic and operations in the construction of the string) and will only be accessed from a single thread, using a StringBuilder is good enough.
  • If your string can change, and will be accessed from multiple threads, use a StringBuffer because StringBuffer is synchronous so you have thread-safety.

Also, using String for logic operations is rather slow, and is not advised at all, since the JVM converts the String to a StringBuffer in the bytecode. A lot of overhead is wasted converting from String to StringBuffer and then back to String again.

  • You use String when an immutable structure is appropriate; obtaining a new character sequence from a String may carry an unacceptable performance penalty, either in CPU time or memory (obtaining substrings is CPU efficient because the data is not copied, but this means a potentially much larger amount of data may remain allocated).
  • You use StringBuilder when you need to create a mutable character sequence, usually to concatenate several character sequences together.
  • You use StringBuffer in the same circumstances you would use StringBuilder, but when changes to the underlying string must be synchronized (because several threads are reading/modifyind the string buffer).

如果要操作少量的数据,用String ;单线程操作大量数据,用StringBuilder ;多线程操作大量数据,用StringBuffer。

不要使用String类的"+"来进行频繁的拼接,因为那样的性能极差的,应该使用StringBuffer或StringBuilder类

为了获得更好的性能,在构造 StringBuffer 或 StringBuilder 时应尽可能指定它们的容量。当然,如果你操作的字符串长度(length)不超过 16 个字符就不用了,当不指定容量(capacity)时默认构造一个容量为16的对象。不指定容量会显著降低性能。

StringBuilder一般使用在方法内部来完成类似"+"功能,因为是线程不安全的,所以用完以后可以丢弃。StringBuffer主要用在全局变量中。

相同情况下使用 StringBuilder 相比使用 StringBuffer 仅能获得 10%~15% 左右的性能提升,但却要冒多线程不安全的风险。而在现实的模块化编程中,负责某一模块的程序员不一定能清晰地判断该模块是否会放入多线程的环境中运行,因此:除非确定系统的瓶颈是在 StringBuffer 上,并且确定你的模块不会运行在多线程模式下,才可以采用StringBuilder;否则还是用StringBuffer。

原文地址:https://www.cnblogs.com/rySZ/p/10546528.html

时间: 2024-12-19 13:16:22

Java String, StringBuilder, StringBuffer的相关文章

【转载】关于Java String, StringBuilder, StringBuffer, Hashtable, HashMap的面试题

REF: http://blog.csdn.net/fightforyourdream/article/details/15333405 题目是一道简单的小程序,像下面这样:[java] view plaincopypublic class Test1 {   public static void main(String args[]) {    String s = new String("Hello");    System.out.println(s);      foo(s);

String StringBuilder StringBuffer 区别

String StringBuilder StringBuffer 区别: String 字符串常量 (可用于少量数据,变化不大的情况) 最慢 不可变类(每次改变都会引起创建新的String类) StringBuffer 字符串变量 线性安全(可多线程使用) 居于中间 可变类 StringBuilder 字符串变量 非线性安全(不支持并发操作,可单线程使用) 最快 可变类

java面试题String,StringBuilder,StringBuffer

面试的经历中,相信大家都经常被问到这三者的区别,说到String我相信绝大多数的人都会说:"String是不可变的,final修饰的",你会看到面试官微微猥琐一笑,接着问到:"final修饰的类就是不可变的吗,那StringBuilder和StringBuffer不是final修饰的?" 1. 先来说说String 看下JDK1.7 String成员变量的源码 /** * @author Lee Boynton * @author Arthur van Hoff *

(转)String StringBuilder StringBuffer 对比 总结得非常好

来源:http://blog.csdn.net/clam_clam/article/details/6831345 转自:http://www.iteye.com/topic/522167 作者:每次上网冲杯Java时,都能看到关于String无休无止的争论.还是觉得有必要让这个讨厌又很可爱的String美眉,赤裸裸的站在我们这些Java色狼面前了.嘿嘿.... 众所周知,String是由字符组成的串,在程序中使用频率很高.Java中的String是一个类,而并非基本数据类型. 不过她却不是普通

String Stringbuilder Stringbuffer的区别

String 字符串常量StringBuffer 字符串变量(线程安全)StringBuilder 字符串变量(非线程安全) 简要的说, String 类型和 StringBuffer 类型的主要性能区别其实在于 String 是不可变的对象, 因此在每次对 String 类型进行改变的时候其实都等同于生成了一个新的 String 对象,然后将指针指向新的 String 对象,所以经常改变内容的字符串最好不要用 String ,因为每次生成对象都会对系统性能产生影响,特别当内存中无引用对象多了以

String StringBuilder StringBuffer 对比 总结得非常好

作者:每次上网冲杯Java时,都能看到关于String无休无止的争论.还是觉得有必要让这个讨厌又很可爱的String美眉,赤裸裸的站在我们这些Java色狼面前了.嘿嘿.... 众所周知,String是由字符组成的串,在程序中使用频率很高.Java中的String是一个类,而并非基本数据类型. 不过她却不是普通的类哦!!! [镜头1] String对象的创建       1.关于类对象的创建,很普通的一种方式就是利用构造器,String类也不例外:String s=new String("Hell

String,StringBuilder,StringBuffer三者的区别

1.三者在执行速度方面的比较:StringBuilder >  StringBuffer  >  String 2.String <(StringBuffer,StringBuilder)的原因 String:字符串常量 StringBuffer:字符串变量 StringBuilder:字符串变量 从上面的名字可以看到,String是"字符创常量",也就是不可改变的对象.对于这句话的理解你可能会产生这样一个疑问  ,比如这段代码: 1 String s = "

String/StringBuilder/StringBuffer之间的区别

String:字符串常量StringBuffer:字符串变量StringBuilder:字符串变量三者在执行速度方面比较:StringBuilder>StringBuffer>String String类型是不可改变的对象,当用String操作字符串时,实际上是不断地创建新的对象,原来的对象就会变成垃圾被GC回收,效率比较低.StringBuffer和StringBuilder是字符串变量,是可以改变的对象,当对字符串操作时,实际上是在一个对象上操作,这样不会像String一样创建一些额外的对

Java String、StringBuffer和StringBuilder类

package com.fish.string; /*  String 字符串类:     题目:new String("abc")创建了几个对象?  两个对象,一个对象是 位于字符串常量池中,一个对象是位于堆内存中.    */ public class Demo1 {     public static void main(String[] args) {         String str1 = "hello";         String str2 =