What is ArrayStoreException in Java?


When you have created an array of a particular data type with fixed size and populate it if you store a value other than its datatype an ArrayStoreException is thrown at the run time.

Example

In the following Java program, we are creating an Integer array and trying to store a double value in it.

 Live Demo

import java.util.Arrays;
public class ArrayStoreExceptionExample {
   public static void main(String args[]) {
      Number integerArray[] = new Integer[3];
      integerArray[0] = 12548;
      integerArray[1] = 36987;
      integerArray[2] = 555.50;
      integerArray[3] = 12548;
      System.out.println(Arrays.toString(integerArray));
   }
}

Runtime Exception

This program gets compiled successfully but, while executing it an ArrayStoreException is thrown.

Exception in thread "main" java.lang.ArrayStoreException: java.lang.Double
   at ther.ArrayStoreExceptionExample.main(ArrayStoreExceptionExample.java:9)
raja
Published on 15-Oct-2019 10:43:20
Advertisements