Raw Type Troubles
A raw type is not type safe. Consider the following statements :
The value of t1 inside rgen is obtained, and this value is cast to Integer. The trouble is that rgen contains a String, not an Integer. However, this cannot be detected at compile time because the type of rgen is unknown. Thus, this statement fails at run time :
A variable of a raw type can be assigned a reference to any type of Gen object. This operation are potentially unsafe. For example :
In the second line, a generic reference is assigned to a raw reference variable. Although this is syntactically correct, it can lead to problems, as illustrated by the fourth line. The setT( ) method of rgen set the t1 to Float which is passed to it. The trouble is that iobj(a reference of type Gen<Integer>) contains a Float value. Thus, at run time, when an attempt is made to assign the contents of iobj to i, a run-time error results because iobj now contains a Float. This error cannot be prevented at compile time. Rather, it causes a run-time error :
A variable of a specific Gen type can be assigned a reference to a raw Gen object. This operation are also potentially unsafe because the type checking mechanism of generics is circumvented. For example :
In the third line, a raw reference is assigned to a generic reference variable. This assignment is syntactically correct, but questionable. Because iobj is of type Gen<Integer>, it is assumed to contain a Integer. However, after the assignment, the object referred to by iobj contains a String. Thus, at run time, when an attempt is made to assign the contents of iobj to i(Integer), a run-time error results because iobj now contains a String. Thus, the assignment of a raw reference to a generic reference bypasses the type-safety mechanism.