Why List<> is not valid but LinkedList<> is valid in java? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Why List<> is not valid but LinkedList<> is valid in java?

and how we know if a class can be generic?

9th Sep 2017, 9:12 PM
Abas Dehnad
Abas Dehnad - avatar
3 Answers
+ 2
The question is kind of fuzzy. Please clarify, is it variable definition or instantiation. In the context I think it's instantiation, so we should say, that List is Interface, that's why we can't create it's instance. The '<>' named 'diamond sign' it was introduced at Java 7 (but I'm not sure about version, but it's certainly that's not later than 7) it's use variable definition to get the type of Generic. In previous Java versions if was something like: "List<Cat> cats = LinkedList<Cat>()" but both generics types should be matched, so in the Java 7 they made a diamond sign as a shortcut.
9th Sep 2017, 9:31 PM
Ilia Kopysov
Ilia Kopysov - avatar
+ 1
I figured it out. my imports were incorrect. I had imported java.awt.List mistakenly
9th Sep 2017, 9:48 PM
Abas Dehnad
Abas Dehnad - avatar
0
first thanks for your answer. from variable definition view 1. List<String> exception tells me that List is not generic 2. also linked list is a subclass of List, how come it can be generic? is this problem relevant to my java version? jdk 8
9th Sep 2017, 9:43 PM
Abas Dehnad
Abas Dehnad - avatar