StringBuilder / StringBuffer与" +"操作者 [英] StringBuilder/StringBuffer vs. "+" Operator

查看:124
本文介绍了StringBuilder / StringBuffer与" +"操作者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读更好,更快,更轻松的Java (由Bruce Tate和Justin Gehtland撰写)并且熟悉敏捷类型团队的可读性要求,例如Robert Martin在他的讨论中所讨论的内容清洁编码书。在我现在的团队中,我被告知明确不要使用 + 运算符,因为它会在运行时创建额外的(和不必要的)字符串对象。

I'm reading "Better, Faster, Lighter Java" (by Bruce Tate and Justin Gehtland) and am familiar with the readability requirements in agile type teams, such as what Robert Martin discusses in his clean coding books. On the team I'm on now, I've been told explicitly not to use the + operator because it creates extra (and unnecessary) string objects during runtime.

但是这个文章,写在'04讨论对象分配如何约10机器指令。 (基本上是免费的)

But this article, Written back in '04 talks about how object allocation is about 10 machine instructions. (essentially free)

它还讨论了GC如何帮助降低这种环境下的成本。

It also talks about how the GC also helps to reduce costs in this environment.

使用 + StringBuilder StringBuffer ? (在我的例子中,它只是 StringBuffer ,因为我们仅限于Java 1.4.2。)

What is the actual performance tradeoffs between using +, StringBuilder or StringBuffer? (In my case it is StringBuffer only as we are limited to Java 1.4.2.)

StringBuffer 对我来说导致丑陋,不太可读的代码,正如Tate的书中的几个例子所示。并且 StringBuffer 是线程同步的,它似乎有自己的成本超过使用 + 运算符的危险 。

StringBuffer to me results in ugly, less readable code, as a couple of examples in Tate's book demonstrates. And StringBuffer is thread-synchronized which seems to have its own costs that outweigh the "danger" in using the + operator.

思考/意见?

推荐答案

使用 String 串联被编译器转换为 StringBuilder 操作。

Using String concatenation is translated into StringBuilder operations by the compiler.

要了解如何编译器正在做我将采用一个示例类,编译它并用jad反编译它以查看生成的字节码是什么。

To see how the compiler is doing I'll take a sample class, compile it and decompile it with jad to see what's the generated bytecode.

原始类:

public void method1() {
    System.out.println("The answer is: " + 42);
}

public void method2(int value) {
    System.out.println("The answer is: " + value);
}

public void method3(int value) {
    String a = "The answer is: " + value;
    System.out.println(a + " what is the question ?");
}

反编译类:

public void method1()
{
    System.out.println("The answer is: 42");
}

public void method2(int value)
{
    System.out.println((new StringBuilder("The answer is: ")).append(value).toString());
}

public void method3(int value)
{
    String a = (new StringBuilder("The answer is: ")).append(value).toString();
    System.out.println((new StringBuilder(String.valueOf(a))).append(" what is the question ?").toString());
}




  • On method1 编译器在编译时执行了该操作。

  • On method2 String 连接相当于手动使用 StringBuilder

  • On method3 字符串连接肯定是坏的,因为编译器正在创建第二个 StringBuilder 而不是重用前一个。

    • On method1 the compiler performed the operation at compile time.
    • On method2 the String concatenation is equivalent to manually use StringBuilder.
    • On method3 the String concatenation is definitely bad as the compiler is creating a second StringBuilder rather than reusing the previous one.
    • 所以我的简单规则是连接是好的,除非你需要再次连接结果:例如在循环中或当你需要时存储中间结果。

      So my simple rule is that concatenations are good unless you need to concatenate the result again: for instance in loops or when you need to store an intermediate result.

      这篇关于StringBuilder / StringBuffer与" +"操作者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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