[JAVA/Android] String, StringBuilder, StringBuffer之间的关系
来源:互联网 发布:淘宝买家秀自慰器图片 编辑:程序博客网 时间:2024/06/05 10:26
在项目中遇到了关于字符串操作的问题,简单比较了String , StringBuilder和StringBuffer之间区别和使用场景,这里做一个总结记录。
1. 这三个类的主要区别,可以从以下两个方面来描述:
Mutability 差别:
String是immutable的,如果改变了String的值,系统就会另外生成一个String对象。于此相反,
StringBuffer
和StringBuilder
是 mutable的,可以直接修改他们的值(内容)而不会生成新的对象。
Thread-Safety Difference:
StringBuffer和String Builder之间最大的区别是:前者是线程安全的,也就是说,可以在多个线程中访问;而后者只能在一个线程中访问。Java较早版本只有StringBuffer,后来加入了String Builder,因为这样可以提高一些运行效率。
2 . 实际使用中,进行选择的原则:
- 如果只是需要一个固定不变的字符串,或者少量的字符串改变操作(尤其是纯字符串的拼接),就用String类即可
- 如果需要对一个字符串做大量的涉及内容改变的操作,而且这些操作在一个线程中完成,就用StringBuilder类
- 如果需要对一个字符串做大量的涉及内容改变的操作,而且这些操作可能在多个线程中完成,就用StringBuffer类
The Basics:
String
is an immutable class, it can't be changed. StringBuilder
is a mutable class that can be appended to, characters replaced or removed and ultimately converted to a String
StringBuffer
is the original synchronized version of StringBuilder
You should prefer StringBuilder
in all cases where you have only a single thread accessing your object.
The Details:
Also note that StringBuilder/Buffers
aren't magic, they just use an Array as a backing object and that Array has to be re-allocated when ever it gets full. Be sure and create yourStringBuilder/Buffer
objects large enough originally where they don't have to be constantly re-sized every time .append()
gets called.
The re-sizing can get very degenerate. It basically re-sizes the backing Array to 2 times its current size every time it needs to be expanded. This can result in large amounts of RAM getting allocated and not used when StringBuilder/Buffer
classes start to grow large.
In Java String x = "A" + "B";
uses a StringBuilder
behind the scenes. So for simple cases there is no benefit of declaring your own. But if you are building String
objects that are large, say less than 4k, then declaring StringBuilder sb = StringBuilder(4096);
is much more efficient than concatenation or using the default which is only 16 characters. If your String
is going to be less than 10k then initialize it with the constructor to 10k to be safe. But if it is initialize to 10k then you write 1 character more than 10k, it will get re-allocated and copied to a 20k array. So initializing high is better than to low.
In the auto re-size case, at the 17th character the backing Array gets re-allocated and copied to 32 characters, at the 33th character this happens again and you get to re-allocated and copy the Array into 64 characters. You can see how this degenerates to lots of re-allocations and copies which is what you really are trying to avoid using StringBuilder/Buffer
in the first place.
This is from the JDK 6 Source code for AbstractStringBuilder
void expandCapacity(int minimumCapacity) { int newCapacity = (value.length + 1) * 2; if (newCapacity < 0) { newCapacity = Integer.MAX_VALUE; } else if (minimumCapacity > newCapacity) { newCapacity = minimumCapacity; } value = Arrays.copyOf(value, newCapacity); }
A best practice is to initialize the StringBuilder/Buffer
a little bit larger than you think you are going to need if you don't know right off hand how big the String
will be but you can guess. One allocation of slightly more memory than you need is going to be better than lots of re-allocations and copies.
Also beware of initializing a StringBuilder/Buffer
with a String
as that will only allocated the size of the String + 16 characters, which in most cases will just start the degenerate re-allocation and copy cycle that you are trying to avoid. The following is straight from the Java 6 source code.
public StringBuilder(String str) { super(str.length() + 16); append(str); }
If you by chance do end up with an instance of StringBuilder/Buffer
that you didn't create and can't control the constructor that is called, there is a way to avoid the degenerate re-allocate and copy behavior. Call .ensureCapacity()
with the size you want to ensure your resulting String
will fit into.
The Alternatives:
Just as a note, if you are doing really heavy String
building and manipulation, there is a much more performance oriented alternative called Ropes .
Another alternative, is to create a StringList
implemenation by sub-classing ArrayList<String>
, and adding counters to track the number of characters on every .append()
and other mutation operations of the list, then override .toString()
to create a StringBuilder
of the exact size you need and loop through the list and build the output, you can even make that StringBuilder
an instance variable and 'cache' the results of .toString()
and only have to re-generate it when something changes.
Also don't forget about String.format()
when building fixed formatted output, which can be optimized by the compiler as they make it better.
- [JAVA/Android] String, StringBuilder, StringBuffer之间的关系
- java中StringBuilder、StringBuffer、String类之间的关系
- java中StringBuilder、StringBuffer、String类之间的关系
- 解释java中StringBuilder、StringBuffer、String类之间的关系
- java中StringBuilder、StringBuffer、String类之间的关系
- java中StringBuilder、StringBuffer、String类之间的关系
- java中StringBuilder、StringBuffer、String类之间的关系
- java中StringBuilder、StringBuffer、String类之间的关系
- java中StringBuilder、StringBuffer、String类之间的关系 - 音
- java中StringBuilder、StringBuffer、String类之间的关系
- java中StringBuilder、StringBuffer、String类之间的关系
- String, StringBuilder, StringBuffer 之间的区别 - Java
- Java:String、StringBuffer、StringBuilder之间的区别
- 测试StringBuilder、StringBuffer、String类之间的关系
- 黑马程序员-StringBuilder、StringBuffer、String类之间的关系
- 全面解释java中StringBuilder、StringBuffer、String类之间的关系
- 全面解释java中StringBuilder、StringBuffer、String类之间的关系
- 全面解释java中StringBuilder、StringBuffer、String类之间的关系
- 在dos中执行文件名的大小写有区别吗?
- 入门级的讲讲soap这个php webservice
- struts2通用标签
- Android开发XML文件解析
- 手动安装hibernate tools
- [JAVA/Android] String, StringBuilder, StringBuffer之间的关系
- 相当好的HTML5 抛砖引玉的文章
- JQuery自学记录2——部分动态效果
- 洒落在雨滴里的灵魂
- CreateCompatibleDC CreateCompatibleBitmap SelectObject
- 总结过去10年的程序员生涯(转自网友)
- animateWithDuration使用详解
- MSDTC组件设置
- lucene-3.6.1简单安装与demo运行 for mac