Let\'s say I have two char
variables, and later on I want to concatenate them into a string. This is how I would do it:
char c1, c2;
// ...
Str
What about
new String(new char[] {a, b})
and if you do it alot you could create a class "Strings" with:
public static String valueOf(char... chars) {
return new String(chars);
}
Your line would then read
String s = Strings.valueOf(a, b);
Nice and short.
Edited
A better name might be:
String s = Chars.asString(a, b);
I think that in "" + var
the +
is actually overloaded to make the conversion:
The Java language provides special support for the string concatenation operator ( + ), and for conversion of other objects to strings. String concatenation is implemented through the StringBuilder(or StringBuffer) class and its append method. String conversions are implemented through the method toString, defined by Object and inherited by all classes in Java. For additional information on string concatenation and conversion
So no difference and no problem from a technical point of view.
Form a readability point of view - it's a matter of personal preference or agreed coding style within the team.
The best way to know is to compile / decompile your code, I used Jad http://en.wikipedia.org/wiki/JAD_(JAva_Decompiler) for that, you will see that your expression was converted into
String s = (new StringBuilder()).append("").append(ci).append(c2).toString();
As you can see javac actually included append("") call, but its cost is negligible, noting is appended to internal StringBuilder buffer, you can check StringBuilder's source
In my opinion, "" + x
is very readable, short, and precise to the point. I'd prefer it to longer constructs like String.valueOf
. The behavior is well defined and it's so commonly in use that I find it hard to call it a hack at all.
The only thing I'd be slightly worried about is performance - and am very positive that it does not matter usually (even though I did not measure or look at the binary). There is also a fair chance that this kind of concat is optimized away, since it should be easy to detect it (this is just a guess though).
The "" + var
style has one very big issue in my opinion. It just uses the toString method of the var
. So if you change the type of var to something else, you will not get an exception. A nice example I just encountered was that the type was changed from int
to Optional<Integer>
. The code still compiles fine, but you get a completely different result.
The problem with that construct is that it usually doesn't express the intent.
It represents concatenation of a String with another value, but concatenation is not usually the goal of this line.
In the specific case that you demonstrated, concatenation is actually the goal, so this code does express the intent.
In the more common use of this approach (String s = "" + intValue;
), the concatentation is merely a tolerated side-effect, while the conversion of intValue
is the actual goal. And a simple String.valueOf(intValue)
expresses that intent much clearer.