views:

206

answers:

6

I'm currentlly refactoring an application using a lot of this:

StringBuffer buff1 = new StringBuffer("");

buff1.append("some value A");
buff1.append("");
buff1.append("some value B");

The coder that made those code lines didn't seems to be an idiot, is there any reasons I can't see to use the append("") to a StringBuffer?

+4  A: 

No you don't need that append(""). You also do not need to initialize the StringBuffer with a String. new StringBuffer(); will work fine.

John Paulett
+4  A: 

Nope.

And given that they're hardcoded strings, it'd be identical to write

buff1 = new StringBuffer("some value Asome value B");

BTW, it's a bit more efficient to use a StringBuilder rather than a StringBuffer (and the API is identical).

Steve B.
Isn't StringBuilder not Thread safe?
lemotdit
it isn't, but how often do you modify the same StringBuffer instance between different threads?
Ken Liu
Yes, but almost always you use it locally in a single thread so you don't need thread safety.
starblue
I was giving the hardcoded String as an example... Because, as I said, it is used in a lot of different situations in the app. And yes, it is used in a Process integration server, in a multi thread situation needing thread safe.
lemotdit
Just because you are using the StringBuffer in a multithreaded application doesn't mean you have to use thread-safe data structures. It only needs to be thread safe if the same instance is shared by multiple threads.
Ken Liu
A: 

The only reason I can think of why he'd do that is readability (though even that seems a bit strange).

Jean Barmash
I consider `String buff1 = "some value A some value B";` a lot more readable than the above
Don
+4  A: 

It's very likely that code was generated by some refactoring tool and the coder wasn't bothered to remove the redundant lines.

Sindri Traustason
in deed it was.
lemotdit
Gotta love variable names like `buff1`.
Tom Hawtin - tackline
+2  A: 

If there are no further appends made to the StringBuffer the code should be rewritten as

String buff1 = "some value some value B";

This is more concise, more readable and safer than:

StringBuffer buff1 = new StringBuffer("");

buff1.append("some value A");
buff1.append("some value B");

I say 'safer' because a String is immutable and a StringBuffer is immutable, so there's no risk of the String accidentally being changed after construction.


Aside

It's a common misconception that "concatenating Strings in Java is bad". While it's true that you shouldn't write code like this

String buff1 = "foo";    
buff1 += "some value A";
buff1 += "some value B";

It's perfectly acceptable to write code like this:

String buff1 = "foo" + "some value A" + "some value B";

when the concatenation is performed in a single statement the code will be optimized to:

String buff1 = "foo some value A some value B";
Don
Shouldn't the compiler be able to rewrite that concatenation of literals as a single literal? There would be no observable difference at run-time.
seh
Yes, per JLS Section 3.10.5, *String Literals*, such a constant expression is rewritten as a single *interned* string.
seh
@seh - well spotted, I'll update!
Don
A: 

He probably meant to put buff1.append(" ");. Just my take.

fastcodejava