Convert a char to a String in Java
This post will discuss how to convert a char to a String in Java.
Converting a char to its equivalent string is very simple and straightforward in Java. We can use any of the following methods to convert a char to a string in Java:
1. Using String.valueOf() method
The standard approach to convert a char to a string in Java is to use the String.valueOf(char) method, which returns the string representation of the specified char argument. This is the most efficient method for converting a char to a string. Here’s an example:
|
1 2 3 4 5 6 7 8 9 |
class Main { public static void main(String[] args) { char ch = 'A'; String s = String.valueOf(ch); System.out.println(s); // "A" } } |
This method basically wraps the specified char in a single-element character array and passes it to the String constructor, as evident from the following snippet. It also overloaded for other primitive types.
|
1 2 3 4 5 |
/* Taken from the source code of `java.lang.String` class (JDK 8) */ public static String valueOf(char c) { char[] data = {c}; return new String(data, true); } |
2. Using Character.toString() method
We can also use the Character.toString() method, which returns a String object representing the specified char value. It is equivalent to calling the String.valueOf() method. Here’s an example:
|
1 2 3 4 5 6 7 8 9 |
class Main { public static void main(String[] args) { char ch = 'A'; String s = Character.toString(ch); System.out.println(s); // "A" } } |
This method internally calls the String.valueOf() method, as evident from the following snippet.
|
1 2 3 4 |
/* Taken from the source code of java.lang.Character class in JDK 8 */ public static String toString(char c) { return String.valueOf(c); } |
3. String Concatenation
Another option is to use the concatenation operator (+) to append a char variable to an empty string literal, which will convert the char value to a string object. Here’s an example:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
class Main { public static String toString(char ch) { return "" + ch; } public static void main(String[] args) { char ch = 'A'; String s = toString(ch); System.out.println(s); // "A" } } |
This looks like a pretty simple approach, but this is the least efficient method because the string concatenation creates a StringBuilder object, and appends the char and the String to it, then calls its toString() method. For example, the above code compiles down to something like:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
class Main { public static String toString(char ch) { return new StringBuilder("").append(ch).toString(); } public static void main(String[] args) { char ch = 'A'; String s = toString(ch); System.out.println(s); // "A" } } |
4. Using String Constructor
We have seen that String.valueOf() uses a single-element character array to wrap the specified character and then pass it to the String constructor. We can directly call the String constructor that takes a char array as an argument. This creates a new String object with the contents of the char array. Here’s an example:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
class Main { public static String toString(char ch) { return new String(new char[]{ch}); } public static void main(String[] args) { char ch = 'A'; String s = toString(ch); System.out.println(s); // "A" } } |
5. Using String.format() method
Finally, we can use the String.format() method that returns a formatted string using the given format string and arguments. For example, we can use the following code to convert a char to a String. Here, the format string "%c" specifies that the argument should be formatted as a character.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
class Main { public static String toString(char ch) { return String.format("%c", ch); } public static void main(String[] args) { char ch = 'A'; String s = toString(ch); System.out.println(s); // "A" } } |
That’s all about converting a char to a String in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)