Java generics and array initialization

后端 未结 4 1285
谎友^
谎友^ 2020-12-01 12:41

What\'s the explanation for the following:

public class GenericsTest {
    //statement 1
    public ArrayList[] lists;

    public GenericsTes         


        
4条回答
  •  时光取名叫无心
    2020-12-01 13:27

    So the actual question is: Why is there no error for declaring a generic array? ?

    You will always get an error at the point you do something erroneous. Adding an error where there isn't technically a problem just adds to a clutter (although an editor might want to point that out to you).

    In some circumstances you may want to bend the rules a bit with an unchecked cast. There's no need to force the code to be littered with more warning suppressions than necessary (other than to point out the folly).

提交回复
热议问题