Overview
Overview
Reification signifies the runtime component representation. Any type which can be completely represented at runtime is known as Reifiable Type
Eg, A primitive type, non-parameterized class or interface type, parameterized type of unbounded wildcard (
), raw type (List), Array whose component is reifiable (
).
Arrays can reify their component types. It means array types can be represented properly at runtime.
Old School Example
Any clue about the problem that the application might encounter?
It suffers from ArrayStoreException at runtime because the actual array object has the component type of Integer which can not store any value of type Double.
Parameterized Array
Let’s have a look at the following snippet to transform a collection to an array..
Is the code correct?
No, it’s not. It will result in a compilation error.
The error occurs due to the type variable of not being a reifiable type. Hence, it reports compilation error at the generic array creation line in the snippet.
Let’s look at another example.
This program also suffers from the same compilation error.
The error happens because List<String%gt; is not a reifiable type.
Solution
So, can’t we use generic arrays for our purpose? No, we can use generic arrays but it is advisable to use any collection type to contain list of data which comprises more operations to be performed on the data container.
I just wanted not to get you inundated with each and every aspect of Array Reification, I kept this post as small as possible so that you can learn gradually. I would love to discuss more on Array Reification in my future posts to cover some of the important aspects on it.