Generic programming in earlier versions of Java needed to use Object as the basic element of a data structure. For example, a generic stack might have the interface
public interface Stack {
public void push(Object item);
public Object pop();
...
}
This has the following drawbacks.
Stack s = new ... // depending how the stack is implemented
s.push("hello");
int len = s.pop().length();
will not compile, because raw objects don't have lengths. It will
be necessary to write
int len = ((String) s.pop()).length();and it is worth noting that, in general, such a type cast only expresses an expectation on the part of the programmer, which may prove false at run-time.