Catching ArrayStoreException at Compile-time

后端 未结 4 1640
悲哀的现实
悲哀的现实 2021-01-20 22:40

Consider the following test of Java\'s ArrayList#toArray method. Note that I borrowed the code from this helpful answer.

public class GenericTest {
         


        
4条回答
  •  [愿得一人]
    2021-01-20 23:14

    The ArrayStoreException is runtime exception not compile time and thrown at run time and it indicates that different type of object is being stored in the array. Object x[] = new String[3]; x[0] = new Integer(0); The only way you can find it at compile time is by using <Integer> type as below

    foo.toArray(new String[10]); 
    

    The above will throw compile time error as The parameterized method toArray(Integer[]) of type List is not applicable for the arguments (String[]).

提交回复
热议问题