views:

78

answers:

2

While compiling a program in Java I got this big WARNING

warning: [unchecked] unchecked call to LinkedList(java.util.Collection) as a member of the raw type java.util.LinkedList

on this line:

LinkedList<Integer> li2 = new LinkedList(li);

What does this warning mean?

Edit:

It should have been infact: LinkedList<Integer> li2 = new LinkedList<Integer>(li);

But still if you please answer the question.

+3  A: 

Raw types are unchecked. Use

LinkedList<Integer> li2 = new LinkedList<Integer>(li);

You should never use raw types in new code. It's only provided for backward compatibility reason. See JLS 4.8

To facilitate interfacing with non-generic legacy code, it is also possible to use as a type the erasure (§4.6) of a parameterized type (§4.5). Such a type is called a raw type.

The use of raw types is allowed only as a concession to compatibility of legacy code. The use of raw types in code written after the introduction of genericity into the Java programming language is strongly discouraged. It is possible that future versions of the Java programming language will disallow the use of raw types.

The emphasis was theirs, not mine.


You added a correction to the original question implying that you want to work with raw types specifically. This seems to defeat the purpose of the type safety provided by generics, but you can always use List<Object> to accomplish this. It's no longer raw, so it's guaranteed to work in the future, although it really doesn't give you any type safety.

Depending on the context, you may also do List<?> for unbounded wildcards.

polygenelubricants
A: 

LinkedList when you call the constructor like this:

LinkedList<Integer> li2 = new LinkedList<Integer>(li);

A raw type is a type that has a type argument, but is used without the argument. It's supported for legacy reasons, but you shouldn't ever use it in new code.

Joachim Sauer
I see no different ?!?
Bright010957
@Bright010957: Sorry, my fault: i forgot the important part.
Joachim Sauer