0
ArrayList<Integer> as = new ArrayList(Arrays.asList("India", "Australia", "South Africa"));

The above mentioned line compiles fine. I am declaring a variable of array list of type Integer, But it stores string values also. I am expecting a compilation error here. Could someone help me please?

Pshemo
  • 118,400
  • 24
  • 176
  • 257
  • By contrast, check what happens when you use `List as = Arrays.asList("India", "Australia", "South Africa");`. Bear in mind that `Arrays.asList` [already returns a list](https://docs.oracle.com/javase/8/docs/api/java/util/Arrays.html#asList-T...-). – andrewJames Jun 13 '21 at 21:32

0 Answers0