ziggy
ziggy

Reputation: 15876

Compiler warnings when declaring Generic types

Why does the compiler issue a warning when declaring a variable as

 List<? extends Object> list = new LinkedList();

Warning:

Note: ZiggyTest.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

But it doesnt issue a warning when i declare the variable as

List<?> list = new LinkedList();

Upvotes: 4

Views: 433

Answers (2)

Lion
Lion

Reputation: 19027

This comes up in Java 5 and later if you're using collections without type specifiers (e.g., Arraylist() instead of ArrayList<String>()). It means that the compiler can't check that you're using the collection in a type-safe manner, using generics.

To get rid of the warning, just be specific about what type of objects you're storing in the collection. So, instead of

List list = new ArrayList();

should be as

List<String> list = new ArrayList<String>();

In your case, if you modify that statement as follows,

List<? extends Object> list = new LinkedList<Object>();

it would compile with no warning because we are now making it type-safe using the generic type (<Object>).

Upvotes: 2

seh
seh

Reputation: 15259

I cannot explain why the compiler won't treat these as equivalent, but given that it does not, I'll attempt to explain why it refuses to do so.

The first one (List<? extends Object>) asserts that the type of objects held in the List are of some unknown type derived from Object. The second one (List<?>) says less; it merely says that the type of the objects in the list is unknown. It doesn't mention any expected supertype as the upper bound of the unknown type.

In order to verify the first assumption, the compiler wants to hear you say something about the expected types held in the List instance constructed here as the raw type LinkedList, which says nothing on the subject. However, if you were to construct the instance as type LinkedList<Object>, you're at least guaranteeing that covariant reads against the instance will be consistent with your assertion: namely, that the things in this list are some kind of Object.

Now, this all seems silly, because every reference/non-primitive type in Java extends Object, so there shouldn't be any difference in interpretation between List<? extends Object> and List<?>; after all, the second one implies the first one, by virtue of the language's type system's mandated singly-rooted class hierarchy.

Upvotes: 1

Related Questions