Overview
Overview
I have previously discussed about different dimensions in Array Reification. Today, I am going to discuss about another important phenomena in Array Reification
Problem Domain
Let’s consider, we have a requirement to store several lists of results in an array container. We can write our program the following way.
If you execute the program, you would encounter the famous Unchecked Cast warning message.
Now let’s try to retrieve Sam’s marks or Dean’s Marks.
We have previously seen that generic array creation is not possible and that’s the reason we needed to use the raw type List to create the container and then we casted it to the required type.
Now, if we try to retrieve the value from the container, we would encounter Class cast exception as the aforementioned code replaces the first element of the array with a list of doubles.
You can see that here we have an array whose component type is List<Integer>. As the component type is not reifiable, the array accepted a List<? extends Number>. For which, the first element of the array got replaced by a list of doubles at runtime.
Principle of Indecent Exposure
Do not expose any array publicly whose compile-time component type in not reifiable
Principle of Truth in Advertising and Principle of Indecent Exposure
In my earlier post, I have discussed about Principle of Truth in Advertising. Both of these principles are quite similar.
Principle of Truth in Advertising tells us that the runtime type of any array should be reifiable whereas Principle of Indecent Exposure tells us that the compile-time component type of any array should be reifiable.