Wildcard
Explore the role of wildcards in Java generics to understand their effect on type safety, element insertion, and extraction limitations. Learn why you can only add null to a List<?> and how lower bounded wildcards can only serve as method arguments, not return types, enhancing your control over generic code behavior.
We'll cover the following...
We'll cover the following...
1.
What is the ? used for in generics?
Show Answer
1 / 2
1.
Will the following method have worked in the previous question for printing a list of type Animal?
static <T extends Animal> void printAnimal(Collection<T> animals) {
for (Animal animal : animals)
animal.speakUp();
}
Show Answer
1 / 2
Technical Quiz
1.
What happens when we try to compile the following snippet?
Collection<?> myColl = new ArrayList<Object>();
myColl.add(new Object());
A.
Compiles
B.
Compiles with warnings
C.
Doesn’t compile
1 / 1
The only element that you can ever insert into a ...