void配列はありません
Javaでよく遊んでいるのでメモ。
void.classが取れるならvoid[].classも取れるのでは?と思ったが取れなかった。
リフレクションでもvoid配列は作れなかった。
package test; import java.lang.reflect.Array; public class VoidArrayTest { public static void main(final String[] args) { Class<Void> clazz = void.class; // コンパイルエラー // Class<?> clazz2 = void[].class; // リフレクションでも作れません Object arr = Array.newInstance(clazz, 3); } }
実行結果はIllegalArgumentException。
Exception in thread "main" java.lang.IllegalArgumentException at java.lang.reflect.Array.newArray(Native Method) at java.lang.reflect.Array.newInstance(Unknown Source) at test.VoidArrayTest.main(VoidArrayTest.java:13)
voidはprimitive型ではなく、例外的にvoid.classと書けるだけ。(以下言語仕様)
The type of p.class, where p is the name of a primitive type (§4.2), is Class<B>, where B is the type of an expression of type p after boxing conversion (§5.1.7).
The type of void.class (§8.4.5) is Class
http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.8.2.