Grokking Java Interview Questions
Vote

0% completed

​

Wildcards and the PECS Rule

A method has the parameter List<Number> amounts and prints the sum of the list. An Integer is a Number, so can a program call this method with a List<Integer> of prices? The compiler rejects the call, although an array of Integer is accepted where an array of Number is expected.

The difference between lists and arrays is the first of four questions. The next two show what a wildcard permits and what it forbids in each direction, and the last one combines both directions into one rule.

Why is List<Integer> not a List<Number>?

Level: Mid-level

**The method `add()

.....

.....

.....

Like the course? Get enrolled and start learning!

Reading Progress

0%


Vote for new content