无法确定最有效的字符串连接

发布于 2025-01-04 19:14:17 字数 368 浏览 1 评论 0 原文

我使用 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;
}

I use toString() method. But I don't know which implemention is better to use and why:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

剩余の解释 2025-01-11 19:14:17

就我个人而言,我会使用后者 - 它更清晰,实际上高效:

  • 对于现代版本的 Java,它将使用不同步的 StringBuilder 类型而不是 StringBuffer< /code>
  • 它不会构造 "Price: " +price"Description: " + description + ";" 的中间字符串,

在 Java 5 下 这是不必要的+我希望后面的代码编译为:

public String toString() {
    StringBuilder builder = new StringBuilder();
    builder.append("Description: ");
    builder.append(description);
    builder.append(";");
    builder.append("Price");
    builder.append(price);
    return builder.toString();
}

重要的一点是第二种形式的清晰度,但是 - 我当然发现它比第一种形式更容易阅读。一个有趣的点是,在编译版本中有两次连续调用附加字符串常量(我已经检查过)。在我看来,这样写会稍微更高效,甚至更具可读性:

public String toString() {
    return "Description: " + description + ";Price: " + price;
}

Personally I'd use the latter - it's clearer and is actually more efficient:

  • For modern versions of Java it'll use the unsynchronized StringBuilder type instead of StringBuffer
  • It won't construct the intermediate strings for "Price: " + price and "Description: " + description + ";" which are unnecessary,

Under Java 5+ I'd expect the latter code to be compiled to:

public String toString() {
    StringBuilder builder = new StringBuilder();
    builder.append("Description: ");
    builder.append(description);
    builder.append(";");
    builder.append("Price");
    builder.append(price);
    return builder.toString();
}

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:

public String toString() {
    return "Description: " + description + ";Price: " + price;
}
江城子 2025-01-11 19:14:17

两者完全相同*,与“+”运算符的连接有效地扩展为您在第一个示例中给出的 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.

烟燃烟灭 2025-01-11 19:14:17

这只是个人喜好,因为它们都是根据 文档

编译器使用字符串缓冲区来实现二进制字符串
连接运算符+。例如代码:

 x = "a" + 4 + "c" 编译为等价的:

 x = new StringBuffer().append("a").append(4).append("c")
                       .toString()     

另外,我会将 @Override 放在您的方法之上。

It's just a personal preference, since it's both compiled the same according to the documentation:

String buffers are used by the compiler to implement the binary string
concatenation operator +. For example, the code:

 x = "a" + 4 + "c"   is compiled to the equivalent of:

 x = new StringBuffer().append("a").append(4).append("c")
                       .toString()     

also, i'd put @Override above your method.

小兔几 2025-01-11 19:14:17

一般来说,最好使用 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文