Convert Character to String in Java

In Java, converting a char value to its equivalent String representation can be achieved using multiple methods. In this blog post, we will explore various techniques with examples. 

1. Using Character.toString() 

Method The Character.toString(char ch) method is the simplest way to convert a char value to its String representation. 

Example:

public class CharToStringExample {
    public static void main(String[] args) {
        char charValue = 'A';
        String stringValue = Character.toString(charValue);

        System.out.println("Character Value: " + charValue);
        System.out.println("String Value: " + stringValue);
    }
}

Output:

Character Value: A
String Value: A

2. Using String.valueOf() Method 

The String.valueOf(char ch) method is another convenient way to convert a char value to its String representation. 

Example:

public class CharToStringExample {
    public static void main(String[] args) {
        char charValue = 'X';
        String stringValue = String.valueOf(charValue);

        System.out.println("Character Value: " + charValue);
        System.out.println("String Value: " + stringValue);
    }
}


Output:

Character Value: X
String Value: X

Conclusion 

Converting a char to a String is a simple task in Java, and you have multiple methods to choose from. Whether you use Character.toString() or String.valueOf() the result will be the same - a String representation of the original char value. 

Related String Conversion Examples

Comments