Add an empty string vs toString - why is it bad?

二次信任 提交于 2019-12-22 04:58:31

问题


According to the tool PMD, the following is a bad practice:

String s = "" + 123; // bad 
String t = Integer.toString(456); // ok 


This is an inefficient way to convert any type to a `String`.

Why is it a bad thing to do?


回答1:


String s = "" + 123; // bad     
String t = Integer.toString(456);

Will be compiled to:

String s = "123";
String t = Integer.toString(456);

so: "" +123 is obvious slightly better! Checked with JAD

public static void main(String args[])
{
//    0    0:ldc1            #16  <String "123">
//    1    2:astore_1
//    2    3:sipush          456
//    3    6:invokestatic    #18  <Method String Integer.toString(int)>
//    4    9:astore_2
//    5   10:getstatic       #24  <Field PrintStream System.out>
//    6   13:new             #30  <Class StringBuilder>
//    7   16:dup
//    8   17:aload_1
//    9   18:invokestatic    #32  <Method String String.valueOf(Object)>
//   10   21:invokespecial   #38  <Method void StringBuilder(String)>
//   11   24:aload_2
//   12   25:invokevirtual   #41  <Method StringBuilder StringBuilder.append(String)>
//   13   28:invokevirtual   #45  <Method String StringBuilder.toString()>
//   14   31:invokevirtual   #48  <Method void PrintStream.println(String)>
//   15   34:return
}

EDIT:

For non-constant values:

int i = 123;
String s = (new StringBuilder()).append(i).toString();
String t = Integer.toString(i);
System.out.println((new StringBuilder(String.valueOf(s))).append(t).toString());

    public static void main(String args[])
    {
    //    0    0:bipush          123
    //    1    2:istore_1
    //    2    3:new             #16  <Class StringBuilder>
    //    3    6:dup
    //    4    7:invokespecial   #18  <Method void StringBuilder()>
    //    5   10:iload_1
    //    6   11:invokevirtual   #19  <Method StringBuilder StringBuilder.append(int)>
    //    7   14:invokevirtual   #23  <Method String StringBuilder.toString()>
    //    8   17:astore_2
    //    9   18:iload_1
    //   10   19:invokestatic    #27  <Method String Integer.toString(int)>
    //   11   22:astore_3
    //   12   23:getstatic       #32  <Field PrintStream System.out>
    //   13   26:new             #16  <Class StringBuilder>
    //   14   29:dup
    //   15   30:aload_2
    //   16   31:invokestatic    #38  <Method String String.valueOf(Object)>
    //   17   34:invokespecial   #44  <Method void StringBuilder(String)>
    //   18   37:aload_3
    //   19   38:invokevirtual   #47  <Method StringBuilder StringBuilder.append(String)>
    //   20   41:invokevirtual   #23  <Method String StringBuilder.toString()>
    //   21   44:invokevirtual   #50  <Method void PrintStream.println(String)>
    //   22   47:return
    }



回答2:


It is inefficient, as it involves an unneeded string concatenation, thus the creation of one or two extra String objects - although I believe the JIT can optimize it away.

To me the bigger problem is that the code is less clear. Calling toString is a standard idiom, understandable to every Java developer (hopefully :-), so you should prefer this.




回答3:


It expands to "" + String.valueOf(yourObject) and thus does an unneeded concatenation. The concatenation involves allocating an extra string and doing an extra copy of the string's value.




回答4:


String s = "" + 123; // bad

The above code creates a temporary string, to combine "" and 123



来源:https://stackoverflow.com/questions/3626917/add-an-empty-string-vs-tostring-why-is-it-bad

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!