Reification

This lesson explains reifiable types in Java.

We'll cover the following...
1.

What is reification in Java?

0/500
Show Answer
Did you find this helpful?
Press + to interact
Java
class Demonstration {
public static void main( String args[] ) {
Integer[] array = new Integer[10];
Number[] refToArray = array;
// Attempt to store a Double in an array of Integers
// throws ArrayStoreException
refToArray[0] = new Double(5);
}
}
1.

What is a reifiable type?

0/500
Show Answer
1 / 2
Press + to interact
Java
import java.util.*;
class Demonstration<T> {
public static void main( String args[] ) {
Integer[] arr = new Integer[10];
System.out.println(arr instanceof Integer[]);
List<Integer> list = new ArrayList<>();
System.out.println(list instanceof List);
// list instanceof List<Integer> <--- compile time error
}
void testInstanceOf(Object o){
// instance of test with the generic type parameter
// System.out.println(o instanceof T); // <--- compile time error
}
}

Attempting to cast an object to a generic type e.g. T t = (T)someObj results in a compiler warning and there's a potential of getting a class cast exception at runtime. Similarly, we can cast a generic parameter to another type e.g. String someObj = (String)t without a compile time warning but a potential for ...