6 ways to convert char to String in Java - Examples

If you have a char value e.g. 'a' and you want to convert it into equivalent String e.g. "a" then you can use any of the following 6 methods to convert a primitive char value into String in Java :

1) String concatenation
2) String.valueOf()
3) Character.toString()
4) Character wrapper class + toString
5) String constructor with char array
6) String.valueOf(char [])

In this article, we will see examples of each approach and learn a little bit more about it. Actually, there is lot of overlap between each method as some of them internally calls String.valueOf(), which eventually calls to a String constructor which accepts char array and creates a String object containing primitive char value with length 1. Once you know, how they work internally, it easy to decide which one is more efficient for purpose.

Char to String using concatenation

This is the easiest way to convert a char to String object in Java. All you need to do is concatenate given character with empty String, as shown in the following example:

char apple = 'a';
String aStr = "" + apple;
System.out.println("char to String using concatenation : " + aStr);   // a

Remark: This is the simplest method to convert a char value to String but it's less efficient and takes more memory than required. Compile translate String concatenation into the following code:
new StringBuilder().append(x).append("").toString();
This is less efficient because the StringBuilder is backed by a char[] of size 16, which is a waste of memory if you just converting one character. This array is then defensively copied by the resulting String

Character to String using String.valueOf()

This is the standard way to convert a char primitive type into String object. If you remember, we have already used the valueOf() method to convert String to int and String to double in Java. Here is the example of using this method to convert char to String:

char boy = 'b';
String bStr = String.valueOf(boy);
System.out.println("char to String using String.valueOf() : " + bStr); // "b"

Remark: String.valueOf(char) is the most efficient method for converting char to String in Java, because it just uses a character array of size one to wrap given character and pass it to String constructor, as shown below (from JDK) :

Char to String conversion with Character.toString()

This is the third example to convert a char primitive value to a String object in Java. In this example, we have used Character.toString() method.

char cat = 'c';
String cStr = Character.toString(cat);
System.out.println("char to String using Character.toString() : " + cStr);

Remark: This method returns a String object representing the given char. It returns a String of length one consisting solely of the specified char. Internally it also calls String.valueOf(char c) method.

BTW, here is nice summary of all the six ways to convert a char value to String object in Java:

6 ways to convert a char to String object in Java

Char to String using Character wrapper class

This is the 4th example of converting a char value to String object in Java. This time, we will use java.lang.Character class, which is a wrapper for char primitive type.

char dog = 'd';
String dStr = new Character(dog).toString();
System.out.println("char to String using new Character() + toString : " + dStr);

Remark: This method returns the String equivalent of Character object's value of length 1. Internally this method wraps the encapsulated char value into a char array and passed to String.valueOf(char[]) method.

String constructor with char array

Here is one more way to convert a char value to String object in Java. In this example, we have wrapped the single character into a character array and passed it to a String constructor which accepts a char array as shown below:

String fStr = new String(new char[]{'f'});
System.out.println("char to String using new String(char array) : " + fStr);

Remark: This is the method which is internally called by String.valueOf() method for char to String conversion. This constructor allocates a new String object to represent a sequence of characters passed via char array argument. The contents of the character array are defensively copied so that subsequent modification of the character array doesn't affect the newly created String.

char to String using String.valueOf(char[])

This the sixth and last way to perform char to String conversion in our article, here is the sample code:
String gStr = String.valueOf(new char[]{'g'});
System.out.println("char to String using String.valueOf(char array) : " + gStr);

Remark: String.valueOf() is overloaded to accept both char and char[]. Since the valueOf() method which accept a char, internally wrap that char into a character array it make sense to use this overloaded method to directly pass a char array, which in turn passed to String constructor which accepts a character array.

That's all about how to convert a char value to String in Java. You can choose any of the above methods, which suits your test but remember, String concatenation is not efficient and can waste a lot of memories if used in the loop. String.valueOf() is more readable and efficient, so should be your default choice. All of the above methods internally uses new String(char[]) though.

Further Learning
Data Structures and Algorithms: Deep Dive Using Java
Java Fundamentals: The Java Language
Complete Java Masterclass

No comments:

Post a Comment