user2440792
user2440792

Reputation: 53

Conversion of arrays with generics

What's the difference between the following statements:

List<E>[] x = (List<E>[]) new List[100];

and

List<E>[] x = new List[100];

In my script it's noted that the former would be a correct way to create an array of generics (although it leads to a compiler warning). But I can't quite figure out what's the use of the (List<E>[]) statement. List<E>[] isn't even it's own type, and the compiler will simply replace it with (List[]), so you'd get

List<E>[] x = (List[]) new List[100];

a conversion from List[] to List[], which is useless. So why put in a (List<E>[]) in the first place?

Upvotes: 5

Views: 145

Answers (3)

jillionbug2fix
jillionbug2fix

Reputation: 52

As in generics object type that is stored in any collection will be type-checked at the point they are added to the collection. Mainly through generics, codes can be understandable by others those who do not need to know about generics. So by inserting checking at compile time and erasing at runtime enables this behaviour.

you can see: http://docs.oracle.com/javase/tutorial/java/generics/

Upvotes: 1

s.d
s.d

Reputation: 29436

This:

List<E>[] x = new List[100];

Says that x is an Array type. Elements of that array are Lists which can hold type E objects. And you are assigning to it an array of lists that can hold any object.

The next statement:

List<E>[] x = (List<E>[]) new List[100];

is no better. Casting won't help. The flaw still is the same.

Ultimately, all of this is an excuse for doing this:

List<E>[] x =  new List<E>[100];

And generics arrays are not allowed in Java. Because Arrays keep their element type at runtime, while generic constructs don't. There must be no array whose element type is not strictly defined.

Problem is due to defining a reference type List<E>[] which, by definition, is not allowed to be instantiated in Java. So, avoid using such types.

You could go with a List of Lists as an alternative.

Upvotes: 1

Dileep
Dileep

Reputation: 5440

Generics add stability to your code by making more of your bugs detectable at compile time.

This is a part from the link that i have given, thought that as important, so i am posting that here

This is a small excerpt from the definitions of the interfaces List and Iterator in package java.util:

public interface List <E> {
    void add(E x);
    Iterator<E> iterator();
}

public interface Iterator<E> {
    E next();
    boolean hasNext();
}

This code should all be familiar, except for the stuff in angle brackets. Those are the declarations of the formal type parameters of the interfaces List and Iterator.

Type parameters can be used throughout the generic declaration, pretty much where you would use ordinary types.

We know the invocations of the generic type declaration List, such as List. In the invocation (usually called a parameterized type), all occurrences of the formal type parameter (E in this case) are replaced by the actual type argument (in this case, Integer).

You might imagine that List stands for a version of List where E has been uniformly replaced by Integer:

public interface IntegerList {
        void add(Integer x);
        Iterator<Integer> iterator();
    }

This intuition can be helpful, but it's also misleading.

It is helpful, because the parameterized type List does indeed have methods that look just like this expansion.

It is misleading, because the declaration of a generic is never actually expanded in this way. There aren't multiple copies of the code--not in source, not in binary, not on disk and not in memory.

A generic type declaration is compiled once and for all, and turned into a single class file, just like an ordinary class or interface declaration.

Type parameters are analogous to the ordinary parameters used in methods or constructors. Much like a method has formal value parameters that describe the kinds of values it operates on, a generic declaration has formal type parameters. When a method is invoked, actual arguments are substituted for the formal parameters, and the method body is evaluated.

When a generic declaration is invoked, the actual type arguments are substituted for the formal type parameters. And that's the >importance of Generics.

You can Look Here for more information about Generics

Upvotes: 3

Related Questions