I can assign char to int as follows.
char c = 'a';
int a = 0;
a = c;
Then, why I can’t assign a char[] to int[]?
int[] ints= new int[4];
char[] chars= new char[4];
ints = chars; // Cannot convert from char[] to int[] ?? But why?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The
chartointpromotion is a special provision for primitive types.Regarding arrays, the Java Language Specification says this:
This works only “if A is a reference type.” Even though
charcan be assigned toint, this rule doesn’t apply because it doesn’t apply to primitive types.So, without any special provision for assigning incompatible types, the assignment fails.
If it were allowed, you’d introduce the possibility of
ArrayStoreExceptionbeing raised on stores to primitive arrays (just like you currently have with arrays of reference types):This happens because
intsis an alias to achar[]that can’t accommodate a 32-bit value. I’m not sure why this is acceptable for reference types, and not for primitives, but it probably has to do with all of the special treatment already required for primitives.