简体   繁体   中英

Effective Java: What is exactly generics array creation warning

Creating an array of generic type objects is not permitted in Java. The array does not has enough type-information to check for ArrayStoreException and ClassCastException based on the type of it at runtime. That's the reason why. However, the first paragraph of Item 24 in Effective java says,

When you program with generics, you will see many compiler warnings: unchecked cast warnings, unchecked method invocation warnings, unchecked generic array creation warnings, and unchecked conversion warnings. The more experience you acquire with generics, the fewer warnings you'll get, but don't expect newly written code that uses generics to compile cleanly.

What is generic array creation warning? if we are to create an array of generic type object, we must get a compile error. why warning? It should the error.

Thank you in advance.

The warning you're referring to is due to the automated array creation that happens in a varargs method, one like this:

public void method(List<String>... listArgs) { ... }

If you call:

method(new ArrayList<String>(), new ArrayList<String>());

You will see the warning (the actual warning text varies according to Java versions, I think):

[unchecked] unchecked generic array creation for varargs parameter of type List<String>[]

What Java does with varargs methods is to create an array of the variable argument parameter, so in this case it will try to create a List<String>[] , which is a generic array and thus the warning message (because it will be declassed to List<?>[] ).

    //create collection and add some elements
    List<LinkedList<Integer>> myList = new LinkedList<LinkedList<Integer>>();
    myList.add(new LinkedList<Integer>());
    myList.add(new LinkedList<Integer>());

    //now if you want to convert it to array, you have to choose the first option 
    //from below (and get a warning), because the second option is unsupported

    //1. warning
    List[] listArray = myList.toArray(new List[myList.size()]); 

    //2. error
    List<Integer>[] listArray = myList.toArray(new List<Integer>[myList.size()]);

So if you make it the only way it is possible (the 1st way), you then have to cast back to type that you had in the first place, and you get another warning of unchecked cast.

The author says " unchecked generic array creation warnings". Something like this will raise a warning, not an error:

Stack<Integer>[] stacks = (Stack<Integer>[]) new Object[10]; // warning: unchecked cast
Stack<Integer>[] stacks2 = new Stack[10]; // warning: unchecked assignment

When you say it should be an error, you're thinking of:

Stack<Integer>[] stacks = new Stack<Integer>[10]; // error: generic array creation

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM