char to String Conversion Example Java


Summary: By the end of this tutorial, "Data type char String Conversion", you will understand converting Java char to string.

We know earlier, parsing is the operation on strings to convert into data types. This parsing, we used in command-line arguments and User Password validation. Now we think the other way. Sometimes, it is required to convert data types to string; exactly opposite of parsing. For this purpose, the String class comes with valueOf() static method. valueOf() is overloaded to take any data type as parameter and converts into string; it converts even objects into strings.

The following program does the job of converting char to String.

public class CharToString
{
  public static void main(String args[])
  {
    char ch = 'A';
    String str = String.valueOf(ch);
    System.out.println("char ch in string form is " + str);
  }
}

The char ch is passed as parameter to valueOf() method. This method converts char ch to string form. Anywhere ch is required as string, str can be used. For example, to display anything thing in TextField, it must be in string form. Now str can be displayed in TextField as follows.

t1.setText(str);

where t1 is an object of TextField.

A string can also be converted to byte, short, int, long, float, double, char and boolean.

2 thoughts on “char to String Conversion Example Java”

Leave a Comment

Your email address will not be published.