5

You cannot convert from int to char, so this would be illegal int i = 88; char c = i;,

However this is allowed char c = 88;.

Isn't a plain number and int literal? How is this allowed?

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
shreyasva
  • 13,126
  • 25
  • 78
  • 101

3 Answers3

8

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.
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • char does not belong to the integer group. My understanding is as follows, since the compiler can check the value of a literal it can perform this conversion smoothly, in other types it can lead to loss of data, is this correct – shreyasva Mar 02 '11 at 10:12
  • 2
    @user265260: Um yes, char *does* belong to the integer group. See http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.2 - "The integral types are byte, short, int, and long, whose values are 8-bit, 16-bit, 32-bit and 64-bit signed two's-complement integers, respectively, and char, whose values are 16-bit unsigned integers representing UTF-16 code units (§3.1)." – Jon Skeet Mar 02 '11 at 10:16
  • Probably a side question but why is `char c = 2.0;` not allowed. – shreyasva Mar 02 '11 at 10:22
  • @user265260: Because that's a constant expression of type `double`. – Jon Skeet Mar 02 '11 at 10:41
4

Actually, converting from int to char is legal, it just requires an explicit cast because it can potentially lose data:

int i = 88; 
char c = (char) i;

However, with the literal, the compiler knows whether it will fit into a char without losing data and only complains when you use a literal that is too big to fit into a char:

char c = 70000; // compiler error
Michael Borgwardt
  • 342,105
  • 78
  • 482
  • 720
0

Its because the literals for integer or smaller than int as byte ,short and char is int. Understand the following in this way.

code:

  byte a = 10;//compile fine
  byte b= 11;//compile fine
  byte c = a+b;//compiler error[says that result of **a+b** is **int**]

the same happens for any mathematical operations as of 'Divide', 'multiply', and other arithmetic operation. so cast the result to get the literal in desired data type

byte c = (byte)(a+b);

So that the same reason why the value int need to have primitive cast to change the value in char. Hope this make some sense.

vinod bazari
  • 210
  • 1
  • 2
  • 10