Conversion between character and int in Java

char is effectively an unsigned 16-bit integer type in Java.

Like other integer types, you can perform an assignment conversion from an integer constant to any integer type so long as it’s in the appropriate range. That’s why

byte b = 10;

works too.

From the JLS, section 5.2:

In addition, if the expression is a
constant expression (ยง15.28) of type
byte, short, char or int :

  • A narrowing primitive conversion may
    be used if the type of the variable is
    byte, short, or char, and the value of
    the constant expression is
    representable in the type of the
    variable.
  • A narrowing primitive
    conversion followed by a boxing
    conversion may be used if the type of
    the variable is :

    • Byte and the value
      of the constant expression is
      representable in the type byte.
    • Short
      and the value of the constant
      expression is representable in the
      type short.
    • Character and the value of
      the constant expression is
      representable in the type char.

Leave a Comment

deneme bonusu veren sitelerbahis casino