For example, for a single method that reads the elements of an array, how can the programmer allow either an array of objects or an array of primitives to be passed as the parameter? Object[] arrayName will only accept an array of Objects, and generic variables do not accept primitives. Is there a way to accept either type of array without overloading the method?
For example, for a single method that reads the elements of an array, how
Share
You can pass an array of either type as an
Object.From there, you have to use reflection. In particular
isArray(),getComponentType(), andisPrimitive()will tell the method what it has been passed. It’s some of the unfortunate untidiness introduced by primitive data types.