1

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;
}
Perception
  • 79,279
  • 19
  • 185
  • 195
Biswas
  • 127
  • 2
  • 4
  • 7

4 Answers4

7

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;
}
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
3

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.

the-banana-king
  • 509
  • 2
  • 9
  • 1
    Not the same. StringBuffer is more optimized. – madmik3 Feb 11 '12 at 16:44
  • 1
    Newer versions of Java (5+) actually use [`StringBuilder`](http://docs.oracle.com/javase/7/docs/api/java/lang/StringBuilder.html) instead of [`StringBuffer`](http://docs.oracle.com/javase/7/docs/api/java/lang/StringBuffer.html) for `String` concatenation with the `+` operator to avoid the overhead of synchronization. – Jeffrey Feb 11 '12 at 16:47
  • @madmik3 Do you have some resources on this? – the-banana-king Feb 11 '12 at 17:05
0

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.

user717572
  • 3,626
  • 7
  • 35
  • 60
0

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

Community
  • 1
  • 1
madmik3
  • 6,975
  • 3
  • 38
  • 60