Convert Char to String.
- Use
String.valueOf(char)
- Use
Character.toString(char)
- Note: this method simply returns a call to
String.valueOf(char)
- Note: this method simply returns a call to
- Use string concatenation
String s = "" + 'c'
- Note: this compiles down to
1
String s = new StringBuilder().append("").append('c').toString();
- which is less efficient because
StringBuilder
is backed bychar[]
(over-allocated by StringBuilder() to 16) and this array will be copied to the resultingString
. On the other hand,String.valueOf(char)
wraps thechar
in a single-element array and passes it to the package private constructorString(char[], boolean)
, which avoids the array copy.1