Can you pass an int array to a generic method in java?

Generics don’t handle primitives in a consistent fashion. This is because Generics are not like templates in C++, it is just a compile time addition to a single class.

When generic are compiled, you end up with Object[] in the above example as the implementing type. As int[] and byte[] etc, do not extend Object[] you cannot use them inter-changeably even if the code involved would be identical (again generics are not templates)

The only class int[] and Object[] share is Object. You can write the above methods Object as the type (see System.arraycopy, Array.getLength, Array.get, Array.set)

Leave a Comment