2

Possible Duplicate:
Generic array creation error

I'm working on an assignment that deals with an Entry array. I figured out how to create it, but I'm not fully understanding how it works. Why is it that when creating the new Entry array, I don't need to specify the K,V type? If you guys could provide some insight as to how it functions, I would greatly appreciate it.

private Entry<K,V>[] data;


data = new Entry[4096];
Community
  • 1
  • 1
kubiej21
  • 692
  • 4
  • 14
  • 29

2 Answers2

3

When you create the array with new Entry[4096] you are just creating 4096 references, the compiler doesn't care at this point that they are references to Entry<K,V> because the will be removed via type erasure. Remember that generics in java are just a syntatic sugar over the underlying class format. At runtime the generics don't exist.

sw1nn
  • 7,070
  • 1
  • 21
  • 35
0

Which part don't you understand? Entry<K,V> is a generic type that is parameterized with K and V. You can also use Entry, which is the raw type without generics (for backwards compatibility). They are the same after type erasure. Similarly, you can have Entry<K,V>[] and Entry[]. They can be converted between each other, but it will be an unchecked conversion.

In fact, you can't specify specific parameters in the component type of an array creation. You must do either new Entry[4096] or new Entry<?,?>[4096]. So an unchecked conversion is unavoidable.

newacct
  • 110,405
  • 27
  • 152
  • 217