Preparing for Java and Spring Boot Interview?

Join my Newsletter, its FREE

6 ways to convert char to String in Java - Example Tutorial

If you have a char value like 'a' and you want to convert it into equivalent String like "a" then you can use any of the following 6 methods to convert a primitive char value into String in Java are String concatenation,  String.valueOf(),  Character.toString() method,  Character wrapper class + toString method, String constructor with a char array, and String.valueOf(char []) method.  In this article, we will see examples of each approach and learn a little bit more about it. Actually, there is a lot of overlap between each method as some of them internally call 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 the purpose.

6 ways to convert a char variable to String in Java

Here is a list of all 6 ways to convert a char to String in Java, you can use the approach you want but pay attention to ease of use and performance impact:

1.Char to String using concatenation in Java

This is the easiest way to convert a char to a String object in Java. All you need to do is concatenate a given character with an 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 a 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

2. Character to String using String.valueOf()

This is the standard way to convert a char primitive type into a 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 a given character and pass it to String constructor, as shown below (from JDK) :

3. 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.

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

6 ways to convert a char to String object in Java

4. Char to String using a Character wrapper class

This is the 4th example of converting a char value to a 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 the Character object's value of length 1. Internally this method wraps the encapsulated char value into a char array and passed it to the String.valueOf(char[]) method.

5. String constructor with a char array

Here is one more way to convert a char value to a 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.

6. 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 accepts a char, internally wrap that char into a character array it makes 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 a 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 use a new String(char[]) though.

Over to you, what is your favorite way to convert char to String in Java? Plus operator, StringBuffer, StringBuilder, StringJoiner or anything else?

No comments:

Post a Comment

Feel free to comment, ask questions if you have any doubt.