无法确定最有效的字符串连接
我使用 toString() 方法。但我不知道哪种实现更好使用以及为什么:
public String toString() {
StringBuffer buffer = new StringBuffer();
buffer.append("Description: " + description + ";");
buffer.append("Price: " + price);
return buffer.toString();
}
public String toString() {
return "Description: " + description + ";" + "Price: " + price;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
就我个人而言,我会使用后者 - 它更清晰,实际上更高效:
StringBuilder
类型而不是StringBuffer< /code>
"Price: " +price
和"Description: " + description + ";"
的中间字符串,在 Java 5 下 这是不必要的+我希望后面的代码编译为:
重要的一点是第二种形式的清晰度,但是 - 我当然发现它比第一种形式更容易阅读。一个有趣的点是,在编译版本中有两次连续调用附加字符串常量(我已经检查过)。在我看来,这样写会稍微更高效,甚至更具可读性:
Personally I'd use the latter - it's clearer and is actually more efficient:
StringBuilder
type instead ofStringBuffer
"Price: " + price
and"Description: " + description + ";"
which are unnecessary,Under Java 5+ I'd expect the latter code to be compiled to:
The important point is the clarity of the second form, however - I certainly find it much simpler to read than the first. One interesting point is that there are two consecutive calls to append with string constants in the compiled version (I've checked). It would be slightly more efficient - and even more readable, IMO - to write:
两者完全相同*,与“+”运算符的连接有效地扩展为您在第一个示例中给出的 StringBuffer 构造。编辑:实际上它是一个 StringBuilder 而不是 Java 5 中的 StringBuffer。唯一的区别是后者是线程安全的并且可以被多个线程访问而无需额外的锁定。尽管如此,只要您确定该对象不在不同线程之间共享,您就应该使用 StringBuilder 来避免同步开销。
(*) 嗯,不完全是这样,如果您在append 方法中嵌套额外的连接,可能会创建不必要的临时字符串,正如Jon Skeet 指出的那样。没有注意到您的代码中的这一点。我的不好。
Both are exactly the same*, concatenation with the '+' operator effectively expands into the StringBuffer construct you gave as the first example.EDIT: Actually it is a StringBuilder rather than a StringBuffer as of Java 5. The only difference is that the latter one is thread-safe and can be accessed by multiple threads without additional locking. Nevertheless it has an synchronization overhead you should avoid by using a StringBuilder whenever you are sure the object is not shared among different threads.
(*) Well, not exactly, if you nest additional concatenations in the append method, unnecessary temporary strings may be created, just as Jon Skeet pointed out. Did not notice that in your code. My bad.
这只是个人喜好,因为它们都是根据 文档:
另外,我会将 @Override 放在您的方法之上。
It's just a personal preference, since it's both compiled the same according to the documentation:
also, i'd put @Override above your method.
一般来说,最好使用 StringBuffer 或 StringBuilder。 StringBuffer 和 StringBuilder 的不同之处在于 StringBuffer 是同步的。在你的例子中我会推荐 StringBuilder。
StringBuilder 的分配频率会降低,当您经常执行此操作时,这可以提供显着的速度和内存管理改进。
请阅读有关此主题的更多信息:
为什么在Java中使用StringBuffer而不是字符串连接运算符
In general it' better to use StringBuffer or StringBuilder. The different between StringBuffer and StringBuilder is that StringBuffer is synchronized. In your example I would recommend StringBuilder.
StringBuilder will allocate less frequently and this can provide significant speed and memory management improvements when you are doing this action a lot.
Please read there is much more on this topic here:
Why to use StringBuffer in Java instead of the string concatenation operator