char is 16 bit unsigned primitive data type where as int is 32 bit signed primitive data type. char can be implicitly converted to an int but where as int cannot be implicitly converted to char because int range is higher than that of char. Explicit casting is needed if we want to assign int value to char.
char c = 3; or int i = 3; c = 1; These both cases are of "Implicit narrowing primitive conversion on assignments" and hence it worked. it to work rules are - 1. The source should be a constant expression either of byte, short, char or int. 2. Also the destination should either be byte, short, char or int. 3. Most important the value should be in range.
To make it work... final int i = 2; // final mentions here that its a constant source... c = i;
On the other hand follwing would not work int i1 = 10; final int i2 = i1; char c = i2; becase, at compile time i2 does not have any value.
Hope it works.
Joined: Mar 13, 2007
1. Sorry... it should be
char c = 3; or int i = 3; ==> this should be final int i = 3 c = 1;
2. In response, I have wrongly written Antonio's name from other thread...