Tuesday, 6 May 2014

When is the ArrayStoreException thrown?

When copying elements between different arrays, if the source or destination arguments are not arrays or their types are not compatible, an ArrayStoreException will be thrown.

class ArrayStoreExceptionEx{
 public static void main(String args[]){
  String myString[] = new String[2];
  Integer myInt[] = new Integer[2];
  
  myString[0] = "Hi";
  myString[1] = "abc";
  
  myInt[0] = 1;
  myInt[1] = 2;

  System.arraycopy(myString, 0, myInt, 0, 1);  
 }
}

Output
Exception in thread "main" java.lang.ArrayStoreException
        at java.lang.System.arraycopy(Native Method)
        at ArrayStoreExceptionEx.main(ArrayStoreExceptionEx.java:12)

                                                             Home

No comments:

Post a Comment