Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is 'new Class[] {iface}' and 'new Class<?>[] {iface}' the same in Java

Tags:

java

Are the two statements equal in Java?

//code 1
Object o1[] = new Class[] {iface};
//code 2
Object o2[] = new Class<?>[] {iface};
like image 856
xianyu1337 Avatar asked Oct 01 '22 10:10

xianyu1337


1 Answers

Yes, it is effectively the same in this case.

Here

Object o1[] = new Class[] {iface};

you're using a raw type with an unparameterized Class type.

Here

Object o2[] = new Class<?>[] {iface};

you're using a parameterized Class type but with a wildcard.

Neither affect what can go into the array.

Since your reference is of type Object[], you can't rely on any further type safety anyway, so they are equivalent.

Vulcan, in the comments, brings up a good point that the raw type will create warnings for you during compilation. You might want to avoid that.

like image 77
Sotirios Delimanolis Avatar answered Oct 05 '22 11:10

Sotirios Delimanolis