Java Char To String

A Java character is a primitive data type where it can only hold just 1 character value, whereas a String can contain multiple characters. A String represents a sequence of characters, while a char only contains a single character. For example, we can only assign the value 'A' to a char, and if we want to add more characters, we then need a String to do that. For example "AB" can't be stored using a char, but a String can. But sometimes, coming from a character, we wish to convert it into a String with only one char item inside. Below we explore some methods and examples on how to convert a Char To String in Java programming language.

Char To String Using String.valueOf()

The String class have a built-in static method that converts a char to a String, and it is called String.valueOf(). Below is a simple example on how to use it:

char c = 'A';
String str = String.valueOf(c);
System.out.println(str);

The String.valueOf method receives the char parameter and then returns a String representation of it. We assign the value to str and will have the below output:
A

Another way of converting a char to String using valueOf, is to pass an array of char to it. For example:

char c = 'B';
String str = String.valueOf(new char[]{c});
System.out.println(str);

And this will do the same thing as we expect, convert a char to String. Hence we get the below output:

B

Char To String Using Character.toString()

Another class that we can use is the Character class, and it have also a toString() method. We show below a simple example for that:

char c = 'C';
String str = Character.toString(c);
System.out.println(str);

Also very similar and it also does the job. We pass a char and get a String in return. Hence we get below output:

C

Char To String Using Character Instantiation

We can choose not to use the static toString() method of the Character class. Instead, we can create an instance of it. And since all Objects implements the toString() method, we can convert it into a String. See below on how to use this method of converting a char to String in Java using Character instantiation.
char c = 'D';
Character instance = new Character(c);
String str = instance.toString();
System.out.println(str);
So there are multiple things happening here. First we created an instance of Character class by passing the primitive char value. And then called the toString() method and store the result to the str variable. Below is what we will expect from it.
D

Char To String Using Empty String Concatenation

This does not look very efficient but it is also another method. We just use the plus sign to concatenate the char with an empty String. Java virtual machine will choose to convert the result into a String behind the scenes. See below as an example:

char c = 'E';
String str = c + "";
System.out.println(str);

So the char 'E' was concatenated to the empty String and results into a String with just one item on it, which is the letter "E". Hence we get below result when we run this code:
E

Char To String Using String Instantiation

One constructor of the String class accepts an array of characters. We can also take advantage of that. Hence we can use String Instantiation to convert a char to String in Java. Pretty neat. See below:

char c = 'F';
String str = new String(new char[]{c});
System.out.println(str);

So we just simply create a String instance passing the char, but putting it behind a char array. We get below output for this:
F

Char To String Using String.format()

The String class have an all around method called format(). We can also use this to convert a char to String. Below is how we use String.format() method to convert a char to String and output in to the console:
char c = 'G';
String str = String.format("%c", c);
System.out.println(str);

So the String.format() method have the directive to expect a char, and we also pass another argument which is the primitive char value. A String representation is returned and we get below:

G

Char To String Using Formatter.format()

Java.util.Formatter is another convenience or helper class that we can use. The behavior is similar to String.format(). Below is an example on how to convert a char to String using Formatter.format() method.

char c = 'H';
Formatter formatter = new Formatter();
String str = formatter.format("%c", c).toString();
System.out.println(str);

So we create first an instance of Formatter class, and we just invoke it's format() method. So we get the expected output below:
H

Char To String Using If Statement

If we can control the range of expected char values, we can just simply write our own if statements for this. See below function that we created as an example:
public static String charToString(char c) {
    if ( c == 'A') return "A"; 
    if ( c == 'B') return "B"; 
    if ( c == 'C') return "C"; 
    if ( c == 'D') return "D"; 
    if ( c == 'E') return "E"; 
    if ( c == 'F') return "F"; 
    if ( c == 'G') return "G"; 
    if ( c == 'H') return "H"; 
    if ( c == 'I') return "I"; 
    if ( c == 'J') return "J"; 
    if ( c == 'K') return "K"; 
    if ( c == 'L') return "L"; 
    if ( c == 'M') return "M"; 
    if ( c == 'N') return "N"; 
    if ( c == 'O') return "O"; 
    if ( c == 'P') return "P"; 
    if ( c == 'Q') return "Q"; 
    if ( c == 'R') return "R"; 
    if ( c == 'S') return "S"; 
    if ( c == 'T') return "T"; 
    if ( c == 'U') return "U"; 
    if ( c == 'V') return "V"; 
    if ( c == 'W') return "W"; 
    if ( c == 'X') return "X"; 
    if ( c == 'Y') return "Y"; 
    if ( c == 'Z') return "Z"; 
    if ( c == 'a') return "a"; 
    if ( c == 'b') return "b"; 
    if ( c == 'c') return "c"; 
    if ( c == 'd') return "d"; 
    if ( c == 'e') return "e"; 
    if ( c == 'f') return "f"; 
    if ( c == 'g') return "g"; 
    if ( c == 'h') return "h"; 
    if ( c == 'i') return "i"; 
    if ( c == 'j') return "j"; 
    if ( c == 'k') return "k"; 
    if ( c == 'l') return "l"; 
    if ( c == 'm') return "m"; 
    if ( c == 'n') return "n"; 
    if ( c == 'o') return "o"; 
    if ( c == 'p') return "p"; 
    if ( c == 'q') return "q"; 
    if ( c == 'r') return "r"; 
    if ( c == 's') return "s"; 
    if ( c == 't') return "t"; 
    if ( c == 'u') return "u"; 
    if ( c == 'v') return "v"; 
    if ( c == 'w') return "w"; 
    if ( c == 'x') return "x"; 
    if ( c == 'y') return "y"; 
    if ( c == 'z') return "z"; 
    if ( c == '0') return "0"; 
    if ( c == '1') return "1"; 
    if ( c == '2') return "2"; 
    if ( c == '3') return "3"; 
    if ( c == '4') return "4"; 
    if ( c == '5') return "5"; 
    if ( c == '6') return "6"; 
    if ( c == '7') return "7"; 
    if ( c == '8') return "8"; 
    if ( c == '9') return "9"; 
    return null;
}

Char To String Using Switch Statement

We can also convert a char to String using a switch statement, see below code for a simple example of doing this. Note that this implementation is not perfect.
public static String charToString(char c) {
    switch (c) {
        case 'A': return "A"; 
        case 'B': return "B"; 
        case 'C': return "C"; 
        case 'D': return "D"; 
        case 'E': return "E"; 
        case 'F': return "F"; 
        case 'G': return "G"; 
        case 'H': return "H"; 
        case 'I': return "I"; 
        case 'J': return "J"; 
        case 'K': return "K"; 
        case 'L': return "L"; 
        case 'M': return "M"; 
        case 'N': return "N"; 
        case 'O': return "O"; 
        case 'P': return "P"; 
        case 'Q': return "Q"; 
        case 'R': return "R"; 
        case 'S': return "S"; 
        case 'T': return "T"; 
        case 'U': return "U"; 
        case 'V': return "V"; 
        case 'W': return "W"; 
        case 'X': return "X"; 
        case 'Y': return "Y"; 
        case 'Z': return "Z"; 
        case 'a': return "a"; 
        case 'b': return "b"; 
        case 'c': return "c"; 
        case 'd': return "d"; 
        case 'e': return "e"; 
        case 'f': return "f"; 
        case 'g': return "g"; 
        case 'h': return "h"; 
        case 'i': return "i"; 
        case 'j': return "j"; 
        case 'k': return "k"; 
        case 'l': return "l"; 
        case 'm': return "m"; 
        case 'n': return "n"; 
        case 'o': return "o"; 
        case 'p': return "p"; 
        case 'q': return "q"; 
        case 'r': return "r"; 
        case 's': return "s"; 
        case 't': return "t"; 
        case 'u': return "u"; 
        case 'v': return "v"; 
        case 'w': return "w"; 
        case 'x': return "x"; 
        case 'y': return "y"; 
        case 'z': return "z";
        case '0': return "0"; 
        case '1': return "1"; 
        case '2': return "2"; 
        case '3': return "3"; 
        case '4': return "4"; 
        case '5': return "5"; 
        case '6': return "6"; 
        case '7': return "7"; 
        case '8': return "8"; 
        case '9': return "9"; 
    }
    return null;
}

Char To String Using Map

If we can control the range of possible char values, we can also create a map of char to String values, and just inquire from this data structure whenever we need to convert a char to String. This is safe and possible as String is immutable Object. When we give one caller an instance, it can't be mutated to affect another part of the code. See below implementation for this.
public static String charToString(char c) {
    Map<Character, String> map = new HashMap<Character, String>();
    map.put('A',  "A"); 
    map.put('B',  "B"); 
    map.put('C',  "C"); 
    map.put('D',  "D"); 
    map.put('E',  "E"); 
    map.put('F',  "F"); 
    map.put('G',  "G"); 
    map.put('H',  "H"); 
    map.put('I',  "I"); 
    map.put('J',  "J"); 
    map.put('K',  "K"); 
    map.put('L',  "L"); 
    map.put('M',  "M"); 
    map.put('N',  "N"); 
    map.put('O',  "O"); 
    map.put('P',  "P"); 
    map.put('Q',  "Q"); 
    map.put('R',  "R"); 
    map.put('S',  "S"); 
    map.put('T',  "T"); 
    map.put('U',  "U"); 
    map.put('V',  "V"); 
    map.put('W',  "W"); 
    map.put('X',  "X"); 
    map.put('Y',  "Y"); 
    map.put('Z',  "Z"); 
    map.put('a',  "a"); 
    map.put('b',  "b"); 
    map.put('c',  "c"); 
    map.put('d',  "d"); 
    map.put('e',  "e"); 
    map.put('f',  "f"); 
    map.put('g',  "g"); 
    map.put('h',  "h"); 
    map.put('i',  "i"); 
    map.put('j',  "j"); 
    map.put('k',  "k"); 
    map.put('l',  "l"); 
    map.put('m',  "m"); 
    map.put('n',  "n"); 
    map.put('o',  "o"); 
    map.put('p',  "p"); 
    map.put('q',  "q"); 
    map.put('r',  "r"); 
    map.put('s',  "s"); 
    map.put('t',  "t"); 
    map.put('u',  "u"); 
    map.put('v',  "v"); 
    map.put('w',  "w"); 
    map.put('x',  "x"); 
    map.put('y',  "y"); 
    map.put('z',  "z"); 
    map.put('0',  "0"); 
    map.put('1',  "1"); 
    map.put('2',  "2"); 
    map.put('3',  "3"); 
    map.put('4',  "4"); 
    map.put('5',  "5"); 
    map.put('6',  "6"); 
    map.put('7',  "7"); 
    map.put('8',  "8"); 
    map.put('9',  "9"); 
    return map.get(c);
}