To be direct here's an example:
ArrayList <?> x = new ArrayList();
ArrayList y = new ArrayList();
x.add("abc"); // Clause 1. Compilation error - No problemo. Understood.
x.addAll(y); // Clause 2. No compilation error
For unbounded wildcard, one cannot add. Why there is no compilation error on the last statement?
Because you are invoking the method with a raw type. The compiler can't perform type checks. If you make y
user generics, addAll(..)
will fail.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With