Question

Here's a question, this first code listing compiles just fine (JDK 1.6 | JDK 1.7):

ArrayList<String> a = new ArrayList<String>();
String[] s = a.toArray(new String[0]);

However, if I declare the List reference as a raw type:

ArrayList a = new ArrayList();
String[] s = a.toArray(new String[0]);

I get a compiler error saying the String[] is required but Object[] was found.

This means my compiler is interpreting the generic method as returning Object[] despite of receiving a String[] as its argument.

I doubled-checked the toArray(myArray) method signature:

<T> T[] toArray(T[] a);

Therefore it is a parameterized method whose type parameter <T> has no relation whatsoever with that of the List (i.e. <E>).

I have no idea how using a raw type here affects the evaluation of parameterized methods using independent type parameters.

  • Does anyone has any idea why this code does not compile?
  • Does anybody knows any reference where this behavior is documented?
Was it helpful?

Solution

It's not exactly what you'd expect, but if you refer to a generic class in raw form, you lose the ability to use generics in any way for instance members. It's not restricted to generic methods either, check out this:

 public class MyContainer<T> {

     public List<String> strings() {
         return Arrays.asList("a", "b");
     }
 }

 MyContainer container = new MyContainer<Integer>();
 List<String> strings = container.strings(); //gives unchecked warning!

This is the relevant part of the JLS (4.8):

The type of a constructor (§8.8), instance method (§8.4, §9.4), or non-static field (§8.3) M of a raw type C that is not inherited from its superclasses or superinterfaces is the raw type that corresponds to the erasure of its type in the generic declaration corresponding to C.

OTHER TIPS

When you don't use generics compiler treats it as a raw type and hence every generic type becomes Object and so you cannot pass String[] because it needs Object[]
So here is the deal - If you use

List l = new ArrayList<String>();

You are using raw type and all its instance members are replaced by its erasure counterparts. In particular each parameterized type appearing in an instance method declaration is replaced with its raw counterpart. See JLS 4.8 for details.

This is the closest description I found in the specification to describe this observed behavior:

The type of a constructor (§8.8), instance method (§8.8, §9.4), or non-static field (§8.3) M of a raw type C that is not inherited from its superclasses or superinterfaces is the erasure of its type in the generic declaration corresponding to C. The type of a static member of a raw type C is the same as its type in the generic declaration corresponding to C.

It is a compile-time error to pass actual type parameters to a non-static type member of a raw type that is not inherited from its superclasses or superinterfaces.

Based on above, and observed behavior, I think its safe to say that all generic parameter types are removed from a raw type. Of course, the use of raw types themselves is discouraged in non-legacy code:

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.

It may be interesting that this behaviour can be "solved". Use two interfaces, a base non generic interface and a generic interface. Then the compiler knows that all functions of the base non generic interface are not generic and will treat them like this.

This behaviour is very annoying if using streams and function chaining and therefore I solve it like following.

Soution via interface inheritence:

public interface GenericInterface<X extends Y> extends BaseInterface
{
    X getValue();
}

public interface BaseInterface
{
    String getStringValue();
}

Now you can do following without warning or problems:

GenericInterface object = ...;
String stringValue = object.getStringValue();

There can be no type parameter's passed into the toArray() method since your ArrayList is a non-parameteterized list, it only knows that it holds Objects, that's it. a.toArray() will always return an Object[] array. Again, you must cast it to (String[]) (with all the dangers therein) if you want to specify that it holds the specific String type.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top