5

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?

3
  • 1
    Care to mention language. Commented Aug 24, 2013 at 16:45
  • Sorry, Question is editied. Don't know about the interface part, I just copied it out of my script, so I assume it's valid code. Commented Aug 24, 2013 at 16:47
  • 1
    It's indeed useless to me. It just transforms the warning from "unchecked conversion" to "unchecked cast". Commented Aug 24, 2013 at 17:10

3 Answers 3

3

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

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

1

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/

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.