6

As par as I know concatinate String using + sign is not a good practice when you have large number of String. But when I check on eclipse generated toString() method (Write click on source file -> Source -> Generate toString() ) it has the same.

public class Temp
 {
      private String tempName;
      private String tempValue;

      // here getters and setters

  /* (non-Javadoc)
         * @see java.lang.Object#toString()
  */
@Override
public String toString() {
    return "Temp [tempName=" + tempName + ", tempValue=" + tempValue + "]";
}

}

Is there any place to configure like my expected toString() method like bellow in eclipse or Why the eclipse doesn't consider that.

   public String expectedToString(){
    StringBuffer sb = new StringBuffer();
    sb.append("Temp [tempName=").append(tempName).append(",").append(" tempValue=").append(tempValue).append("]");
    return sb.toString();
}

I'm going to use auto generated toString() method to log my object values.

Kindly advice me.

someone
  • 6,577
  • 7
  • 37
  • 60
  • IIRC 1) Now the best way is using `StringBuilder` and 2) nowadays the compiler changes the `String +` to `StringBuilder` expressions. Can someone confirm it? – SJuan76 Dec 30 '12 at 04:56
  • 2
    Using + to concatenate isn't really a problem any more. The compiler optimizes the use of +, especially when multiple strings are involved. – Chris Gerken Dec 30 '12 at 04:58
  • 1
    Non-issue: "+" and ".append()" and ".concat()" should perform identically in this case. Interesting links: http://www.vogella.com/blog/2009/07/19/java-string-performanc/ and [Yet again on string append vs concat vs +](http://stackoverflow.com/questions/8962482/yet-again-on-string-append-vs-concat-vs) – paulsm4 Dec 30 '12 at 05:03

2 Answers2

8

No need to change anything, it's compact and easily readable, javac will use StringBuilder for actual concatination, if you decompile your Temp.class you will see

public String toString() {
   return (new StringBuilder("Temp [tempName=")).append(tempName).append(", tempValue=").append(tempValue).append("]").toString();
}

But in other situations, like

    String[] a = { "1", "2", "3" };
    String str = "";
    for (String s : a) {
        str += s; 
    }

+ or += is a real performance killer, see decompiled code

String str = "";
for(int i = 0; i < j; i++) {
    String s = args1[i];
    str = (new StringBuilder(String.valueOf(str))).append(s).toString();
}

on each iteration a new StringBuilder is created and then converted to String. Here you should use StringBuilder explictily

    StringBuilder sb = new StringBuilder();
    for (String s : a) {
        sb.append(s); 
    }
    String str = sb.toString();
Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
0

Note that Eclipse 4.21 (Jul. 2021, 9 years later) would facilitate the production of the right code with:

"Use StringBuilder instead of StringBuffer" clean up

A new clean up has been added that converts code to use StringBuilder (added in Java 1.5) rather than StringBuffer which has synchronized methods and is slower than using StringBuilder.

There is a sub-option where changes will only occur to local variables which is on by default.
When this sub-option is on, changes will only occur to StringBuffer variables that are local to a method.
When a method calls other methods with such variables or assigns to/from fields/parameters, the method contents will not be converted.
It is permitted to append StringBuffer fields or parameters to local StringBuffer variables which can then still be converted to StringBuilder.

When the option is selected and the sub-option for local variables is off,all usage of StringBuffer in the selected files are changed to StringBuilder regardless of usage.
It should be noted that in this case, the clean up will not track down non-selected classes and methods that are referenced and might require changes to compile successfully.

To apply the clean up, select the Use StringBuilder instead of StringBuffer check box on the Optimization tab in your clean up profile.
To specify just for local variables, select the Only for local variables check box found just below.

Preferences -- https://www.eclipse.org/eclipse/news/4.21/images/stringbuffer-to-stringbuilder-prefs.png

Example:

Before -- https://www.eclipse.org/eclipse/news/4.21/images/stringbuffer-to-stringbuilder-before.png

Would become:

After -- https://www.eclipse.org/eclipse/news/4.21/images/stringbuffer-to-stringbuilder-after.png

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250