Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

<? extends > Java syntax

Tags:

java

generics

This code:

List<? extends Reader> weirdList;
weirdList.add(new BufferedReader(null));

has a compile error of

The method add(capture#1-of ? extends Reader) in the type List is not applicable for the arguments (BufferedReader)

Why? BufferedReader extends reader, so why isn't that a "match"?

like image 766
Jeremy Avatar asked Apr 02 '13 22:04

Jeremy


2 Answers

List<? extends Reader> weirdList can hold reference to any type of List that stores any type of Reader. So it is possible that

List<? extends Reader> weirdList1 = new ArrayList<BufferedReader>();
List<? extends Reader> weirdList2 = new ArrayList<FileReader>();

If Java would allow you to add BufferedReader to weirdList1 it would also have to let you add BufferedReader to weirdList2 (reference type is the same) which is not suppose to happen since weirdList2 should store only FileReaders.

like image 105
Pshemo Avatar answered Oct 26 '22 11:10

Pshemo


When the compiler sees <? extends Reader>, then it assumes that it could be any type that is or extends Reader. It could be something that is imcompatible with BufferedReader, such as StringReader. So if the generic type in the class definition shows up in the parameter of a method such as add, and the type of the instance has something like <? extends Something>, the compiler must disallow it for type safety reasons. In this example, it could be List<StringReader>, so you shouldn't be able to add a BufferedReader here.

like image 26
rgettman Avatar answered Oct 26 '22 09:10

rgettman