Myang310
Myang310

Reputation: 63

Java Generics Type Casting Necessary?

This is the code from my textbook:

Stack<String>[] a = (Stack<String>[]) new Stack[N];

My questions are:

  1. Why is it "new Stack[N]"?
  2. Why do you have to do type conversion on the new stack array created? I tried it with just

    Stack<String>[] a = new Stack[N];
    

and it compiled and ran fine. Even after pushing Strings into a and printing the pop method. Also, pushing a int in would immediately give me a compiler error so why is it necessary to type cast it to

Stack<String>[] 

specifically?

Upvotes: 6

Views: 493

Answers (3)

newacct
newacct

Reputation: 122439

First, as you've noticed, the cast is unnecessary. Without the cast, you will get an unchecked conversion warning (whereas with the cast, you will get an unchecked cast warning). The conversion between a raw type and a parameterized type can be made without an explicit cast (you will just get a warning).

If you are asking about why it's not possible to directly create an array of a parameterized type, this is because of how arrays in Java guarantee that, at runtime, they will never hold an element that is not an instance of the component type the array was created with. i.e. a runtime check is performed every time something is put into the array to check whether it is an instance of the component type of the array.

As you may know, generic type arguments don't exist at compile time, and it's not possible to check instanceof a parameterized type at runtime. It's only possible to check instanceof reified types. If an array could be created with a parametrized type, it would not be able to fulfill its contract of checking that everything put into it at runtime is an instance of that type. Someone relying on this guarantee of arrays will get unexpected results. Therefore, it will not allow you to do it. Creating an array with a raw type (which is what you did), or with a wildcard-parametrized type, however, is okay because those are reified types. Although you are using it as an array of a parameterized type, you did that through an unchecked conversion (with a warning) so you take responsibility for any safety issues that may result.

Upvotes: 0

gustf
gustf

Reputation: 2017

  1. Not sure what you mean by that question, but if you mean why not new Stack<String>[N] it is because java arrays does not support parameterized array creation.

  2. Because of my answer in (1) the created array is not parameterized, but you are assigning it to a parameterized variable. And therefor the optional, but preferred checked cast.

Upvotes: 1

zer0chain
zer0chain

Reputation: 4457

You can not create arrays with parameterized type (see Restrictions on Generics). So you can not use ... = new Stack<String>[N]. Using ... = new Stack[N] will work but you will have a warning for unchecked convertion (use -Xlint in javac to see the warning).

So the only proper way is to create the array with a raw parameter and then to apply a checked cast to the wanted type.

Upvotes: 4

Related Questions