Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 319
user2440792 Avatar asked Aug 24 '13 16:08

user2440792


1 Answers

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

like image 90
Dileep Avatar answered Sep 23 '22 14:09

Dileep