Why does the following code throw ArrayStoreException?
double[] a = {2.0,3.4,3.6,2.7,5.6};
int[] b = {2,3,4,5};
System.arraycopy(b,0,a,1,4);
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.
From the docs for
System.arraycopy:That’s exactly the case here –
intanddoubleare different primitive types, so the exception is thrown as documented.The point of
arraycopyis that it can work blindingly fast by copying the raw data blindly, without having to apply any conversions. In your case it would have to apply conversions, so it fails.