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;
// ...
String s = "" + c1 + c2;
I've seen people who say that the "" +
"trick" is "ugly", etc, and that you should use String.valueOf
or Character.toString
instead. I prefer this construct because:
- I prefer using language feature instead of API call if possible
- In general, isn't the language usually more stable than the API?
- If language feature only hides API call, then even stronger reason to prefer it!
- More abstract! Hiding is good!
- I like that the
c1
andc2
are visually on the same levelString.valueOf(c1) + c2
suggests something is special aboutc1
- It's shorter.
Is there really a good argument why String.valueOf
or Character.toString
is preferrable to "" +
?
Trivia: in java.lang.AssertionError
, the following line appears 7 times, each with a different type:
this("" + detailMessage);