我们什么时候应该将String更改为Stringbuilder? [英] When should we change a String to a Stringbuilder?

查看:190
本文介绍了我们什么时候应该将String更改为Stringbuilder?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在应用程序中,String是常用的数据类型。我们所知道的是,String的变异会占用大量内存。所以我们可以做的是使用StringBuilder / StringBuffer。

In an application a String is a often used data type. What we know, is that the mutation of a String uses lots of memory. So what we can do is to use a StringBuilder/StringBuffer.

但是我们应该在什么时候更改为StringBuilder?

我们应该做什么,当我们必须拆分它或重新设置那里的字符时?

But at what point should we change to StringBuilder?
And what should we do, when we have to split it or to remplace characters in there?

例如:

 //original:
 String[] split = string.split("?");  
 //better? :  
 String[] split = stringBuilder.toString().split("?);

 //original:
 String replacedString = string.replace("l","st");  
 //better? :  
 String replacedString = stringBuilder.toString().replace("l","st");  
 //or  
 StringBuilder replacedStringBuilder = new StringBuilder(stringBuilder.toString().replace("l","st);


推荐答案


我们所知道的是,String的变异会占用大量内存。

What we know, is that the mutation of a String uses lots of memory.

这是不正确的。字符串不能变异。它们是不可变的。

That is incorrect. Strings cannot be mutated. They are immutable.

你实际谈论的是从其他字符串构建一个字符串。这可以使用比必要更多的内存,但它取决于如何你构建字符串。

What you are actually talking about is building a String from other strings. That can use a lot more memory than is necessary, but it depends how you build the string.


所以我们能做的是使用StringBuilder / StringBuffer。

So what we can do is to use a StringBuilder/StringBuffer.

在某些情况下使用StringBuilder会有所帮助:

Using a StringBuilder will help in some circumstances:

  String res = "";
  for (String s : ...) {
      res = res + s;
  }

(如果循环迭代很多次,那么优化上面的使用StringBuilder 可能值得。)

(If the loop iterates many times then optimizing the above to use a StringBuilder could be worthwhile.)

但在其他情况下,这是浪费时间:

But in other circumstances it is a waste of time:

  String res = s1 + s2 + s3 + s4 + s5;

(优化上述内容以使用StringBuilder是浪费时间,因为Java编译器会自动将表达式转换为创建和使用StringBuilder的代码。)

(It is a waste of time to optimize the above to use a StringBuilder because the Java compiler will automatically translate the expression into code that creates and uses a StringBuilder.)

当需要访问和/或更新字符串时,您应该只使用StringBuffer而不是StringBuilder由多个线程组成;即当需要是线程安全的时候。

You should only ever use a StringBuffer instead of a StringBuilder when the string needs to be accessed and/or updated by more than one thread; i.e. when it needs to be thread-safe.


但是我们应该在什么时候更改为StringBuilder?

But at what point should we change to StringBuilder?

简单的答案是在探查器告诉您时执行此操作你的字符串处理/处理中存在性能问题。

The simple answer is to only do it when the profiler tells you that you have a performance problem in your string handling / processing.

一般来说,StringBuilders用于构建字符串而不是作为主要表示形式的字符串。字符串。

Generally speaking, StringBuilders are used for building strings rather as the primary representation of the strings.


当我们必须拆分或替换那里的字符时,我们该怎么办?

And what should we do, when we have to split it or to replace characters in there?

然后你必须检查你决定使用StringBuilder / StringBuffer作为你的主要代表。如果仍有必要,您必须弄清楚如何使用您选择的API进行操作。 (这可能需要转换为String,执行操作,然后从结果中创建新的StringBuilder。)

Then you have to review your decision to use a StringBuilder / StringBuffer as your primary representation at that point. And if it is still warranted you have to figure out how to do the operation using the API you have chosen. (This may entail converting to a String, performing the operation and then creating a new StringBuilder from the result.)

这篇关于我们什么时候应该将String更改为Stringbuilder?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆