Question

How do I convert from an int to a char?

Answer

If you aren't interested in converting from an int to a string (from int 127 to string "127"), and only want to convert to an ASCII value, then you only need to cast from an int to a char.

What's casting? Casting is when we explicitly convert from one primitve data type, or a class, to another. Here's a brief example.

public class int_to_char
{
      public static void main(String args[])
      {
           int  a = 65;
           char myChar = (char) a; // cast from int to char

           System.out.println ("Char - " + myChar);
      }
}

In this example, we have a number (65), which represents ASCII character A. We cast the number from an integer to a char, converting it to the letter A.

Related questions

How do I convert from a char to an int?


Back