Is concatenating with an empty string to do a string conversion really that bad?
Posted
by polygenelubricants
on Stack Overflow
See other posts from Stack Overflow
or by polygenelubricants
Published on 2010-03-24T09:23:05Z
Indexed on
2010/03/24
9:53 UTC
Read the original article
Hit count: 222
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);
© Stack Overflow or respective owner