One of my readers asked me about the difference between ArrayList vs ArrayList< in Java?>, which was actually asked to him on a recent Java development interview. The key difference between them is that ArrayList is not using generics while ArrayList is a generic ArrayList but they looks very similar. If a method accepts ArrayList or ArrayList<?> as a parameter then it can accept any type of ArrayList e.g. ArrayList of String, Integer, Date, or Object, but if you look closely you will find that one is raw type while other is using an unbounded wildcard. What difference that could make? Well, that makes a significant difference because ArrayList with raw type is not type safe but ArrayList<?> with the unbounded wildcard is type safe.
You can add objects of any type into raw ArrayList but you cannot do that with a generic ArrayList with unbounded wildcard i.e. ArrayList, it will be a compile-time error, as we'll see by a code example in this article.
This is one of the very interesting Java Interview questions from Generics and Collection, but unfortunately, I didn't have that one in my list of Java Generics Interview questions but will add it soon.
You can add objects of any type into raw ArrayList but you cannot do that with a generic ArrayList with unbounded wildcard i.e. ArrayList, it will be a compile-time error, as we'll see by a code example in this article.
This is one of the very interesting Java Interview questions from Generics and Collection, but unfortunately, I didn't have that one in my list of Java Generics Interview questions but will add it soon.