Wednesday, July 14, 2021

How to convert Char to String in Java with Example

There are multiple ways to convert char to String in Java. In fact, String is made of a Character array in Java, which can be retrieved by calling the String.toCharArray() method. Char is a 16 bit or 2 bytes unsigned data type in java mainly used to store characters. You can convert a single character into a String for any purpose in Java in more than one way. I thought about this article while writing how to convert Integer to String in Java but t it took a little longer to get this post completed. In this Java tutorial, we will see different ways to convert a character into a String in java with code Examples.

4 ways to convert Char to String in Java

Here is a list of 4 different ways I am aware of converting Character to String in Java, most of them are quite easy and don’t require much code but these Java examples are very helpful if you are new to Java programming.

Character to String Example 1: Character.toString

Character class provides a convenient toString() method which takes a character and return its String equivalent. toString() is static method in Character class and can be accessed using class name like Character.toString(). here is an example of converting char to String using Character.toString():

char ch = 'U';
String charToString = Character.toString(ch);



Character to String Example 2: String concatenation operator

Java + can be used to concatenate String but it can also concatenate a String and a character and can result in another String. This is another shortcut you can use to convert Character into String in Java. here is an example of converting character to String in Java:

char ch = 'U';
String str = "" + ch;

Character to String Example 3: using Anonymous Array

Anonymous array in Java can be used to wrap a single character into a char array and then passing that array into a String constructor. a new String will be created from that character. see below for char to String example in Java:

char ch = 'U';
String fromChar = new String(new char[]{ch});

Character to String Example 4: using String.valueOf()

String.valueOf() is another neat and clean way of converting character into String in Java. I like this method
because its simple and straightforward or I say neat and clear. see the following character to String conversion example in Java

char ch = 'U';
String valueOfchar = String.valueOf(ch);



Code Example of Converting Char to String in Java

This section contains a complete code example of converting char to String in Java by using all four methods mentioned above. In my experience concatenation operator seems to be most popular but Character.toString() or String.valueOf() is my favorite way of converting a character to String object in java.

public class CharToStringExample {

       public static void main(String args[]) {
              char ch = 'U';

              // char to string using Character class
              String charToString = Character.toString(ch);
              System.out.println("Converting Char to String using Character class: " + charToString);

              // char to String using String concatenation
              String str = "" + ch;
              System.out.println("Converting Char to String using String concatenation: " + str);

              // char to String using anonymous array
              String fromChar = new String(new char[] { ch });
              System.out.println("Converting Char to String using anonymous array: " + fromChar);

              // char to String using String valueOf
              String valueOfchar = String.valueOf(ch);
              System.out.println("Converting Char to String using String valueOf: " + valueOfchar);

       }

}

Output:
Converting Char to String using Character class: U
Converting Char to String using String concatenation: U
Converting Char to String using an anonymous array: U
Converting Char to String using String valueOf: U


That’s all on how to convert Char to String in Java with four different examples. For many of us it's a trivial operation but for many beginners, it's quite helpful to know various ways to change the character into String.


Other Java String tutorials you may find interesting

4 comments :

Anonymous said...

Character.toString() is the best option for converting character to String into Java. if you want to convert character array to String than you can directly pass that to String Constructor as:

char[] cArray = {'a','b','c'};
System.out.println("Char array to String Java Example: " + new String(cArray));

Anonymous said...

Character.toString(ch) references String.valueOf(ch)

Anonymous said...

The concatenation method is very useful when the .toString method is not available to you. Thanks for reminding me about this. I learned how to concatenate but forgot how to do so.

Unknown said...

Thank u very much.This blogspot is very useful to me.I learned character to string conversion.

Post a Comment