0

In the below syntax from java.util.HashMap, Generic type parameters are used for type casting after instantiating a raw type array,

Node<K,V>[] newTab = (Node<K,V>[])new Node[newCap];

Similar code does not compile here with similar syntax(as shown below), giving error: Type safety: Unchecked cast from Node[] to Node<K,V>[]

public class NestedInterfaceInInterface {

    public static void main(String[] args) {
        Node<K,V>[] newTab = (Node<K,V>[])new Node[10];
        //Node<String,String>[] newTab = (Node<String,String>[])new Node[10];  // this works


    }
}

1) How do I resolve this error ?

2) How syntax Node<K,V>[] newTab = (Node<K,V>[])new Node[10]; different from Node<String,String>[] newTab = (Node<String,String>[])new Node[10];?

overexchange
  • 11,586
  • 11
  • 75
  • 203

2 Answers2

3

The problem is that you can't create an array of a generic type in Java so you are forced to cast a raw type array of Node[] to a Node<K,V>[] by hand.

The operation is called type conversion and it is used in multiple occasions. In this circumstance you are doing an unsafe downcast which will yield a warning

Type Safety: unchecked cast from Node[] to Node<K,V>[]

But you can suppress it in case. Mind that there is no way to prevent this warning and you could consider using a List<Node<K,V>> which wouldn't have such problems.

Community
  • 1
  • 1
Jack
  • 125,196
  • 27
  • 216
  • 324
  • Firstly, why do we need `static class Node implements Map.Entry { }`? when we want to create raw type array `new Node[newCap]`. Can it be `static class Node implements Map.Entry{}`? – overexchange Sep 05 '15 at 14:59
  • 2
    You don't want a raw type array, you want a generic type array but you can't create it directly. – Jack Sep 05 '15 at 15:01
  • What is the difference between `Node[] newTab = (Node[])new Node[newCap];` **and** `Node[] newTab = new Node[newCap];`? – overexchange Sep 05 '15 at 15:04
  • 1
    The difference is that the latter won't compile because __you can't create an array of generic objects__ as stated in my answer. – Jack Sep 05 '15 at 15:15
  • As you said: "there is no way to prevent this warning", but how is this syntax working in `java.util.HashMap`?`Node[] newTab = (Node[])new Node[newCap];` – overexchange Sep 05 '15 at 17:46
0

The reason why generic arrays are not allowed is due to Java's type erasure. Even Collections such as List<T> merely casts between Object and T when objects are accessed. The most typesafe way of viewing generics is using Collections.checkedCollection which takes Collection<E> and Class<E> and checks the type at runtime, preventing the compiler-issued warnings at compile time.

M. Shaw
  • 1,732
  • 9
  • 15
  • the question is, line `Node[] newTab = (Node[])new Node[newCap];` works in `java.util.HashMap` but like `Node[] newTab = (Node[])new Node[10];` does not work in this [example](https://github.com/shamhub/Java_programming/blob/master/JavaCode/src/StaticMemberClasses/NestedInterfaceInInterface.java). why? – overexchange Sep 07 '15 at 04:26
  • 1
    @overexchange Because you haven't specified the type parameters `K,V` in the class declaration? Change it to `public class NestedInterfaceInInterface`. – M. Shaw Sep 07 '15 at 04:28
  • That won't work either. They're attempting the code in a `static` method. Make the method generic or introduce the type parameters some other way. – Sotirios Delimanolis Sep 07 '15 at 08:41