Convert char to string java

Let’s learn how do I convert a char to a string in java?

Convert char to string java

Here we are going to convert character to string using Character.toString() method of Character class. Character.toString() method returns a String object representing the specified char.

The result is a string of length1 consisting solely of the specified char. Let’s see an example on converting a char to a string.

public class CharToStringJava
{
   public static void main(String[] args)
   {
      char ch ='G';
      String str = Character.toString(ch);
      System.out.println("char to string is: " + str);
   }
}

Output:

char to string java is: G


Similarly we can also use String.valueOf(char c) method to convert char to string. This method returns a string of length 1 containing as its single character the argument c. Let’s see an example on how to convert char to string.

public class CharToStringExample
{
   public static void main(String[] args)
   {
      char ch = 'F';
      String str = String.valueOf(ch);
      System.out.println("After conversion is: " + str);
   }
}

Output:

After conversion is: F


Also read – major features of java