Convert Char to String in Java

There are multiple ways to convert char to String in Java. Here is a list of the 3 most used ways:

  • Using the Character.toString() method
  • Using the String.valueOf() method
  • Using the String constructor that accepts char array

Convert char to String using the Character.toString() method

We can use the toString() method from the Character Wrapper class like in the example below:

public class Test {

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

    String str = Character.toString(ch); // returns String representation of the provided char
  }
}

Convert char to String in Java using the String.valueOf() method

There is a valueOf() method from the String class that returns the String representation of the char argument.

public class Test {

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

    String str = String.valueOf(ch);
  }
}

Using the String constructor that accepts char array

We can also convert char to String by creating a new String object and providing char array as an argument. See the following example:

public class Test {

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

    String str = new String(new char[]{ch});
  }
}

Here, we passed an anonymous char array to the constructor of the String class, and we populated it with our input char. This constructor returns a String representation of the provided char array.

That’s it!

Leave a Reply

Your email address will not be published.