I have a char
and I need a String
. How do I convert from one to the other?
Java – How to convert a char to a String
charjavastringtype-conversion
Related Question
- Java – How to read / convert an InputStream into a String in Java
- Python – Convert bytes to a string
- Javascript – How to make the first letter of a string uppercase in JavaScript
- Javascript – How to replace all occurrences of a string in JavaScript
- Javascript – How to check whether a string contains a substring in JavaScript
- Java – How to convert a String to an int in Java
- Java – Why is char[] preferred over String for passwords
- Java – Why is processing a sorted array faster than processing an unsorted array
Best Solution
You can use
Character.toString(char)
. Note that this method simply returns a call toString.valueOf(char)
, which also works.As others have noted, string concatenation works as a shortcut as well:
But this compiles down to:
which is less efficient because the
StringBuilder
is backed by achar[]
(over-allocated byStringBuilder()
to16
), only for that array to be defensively copied by the resultingString
.String.valueOf(char)
"gets in the back door" by wrapping thechar
in a single-element array and passing it to the package private constructorString(char[], boolean)
, which avoids the array copy.