Wong Martin wrote:Why does Java offer its user an opportunity to be wrong?
Like I said, because of backward compatibility. A lot of code was written in Java before version 1.5, when generics were introduced. Let's take an example. the java.util.List class exists since version 1.2. Let's say you made a program before Java 1.5, that used a List. Generics didn't exist, so your program would just use raw types.
When 1.5 came around the corner, the designers chose to change classes like List to use generic types, so you could do compile time type checking. However, if they forced you to parameterize all your types properly, this meant your old program would suddenly break.
Now, what the designers could have done instead, was create a new class for *every* type that uses generics, e.g. GenericList, and deprecate the raw types. You can imagine this is a lot of work, and makes the API very bloated. Instead, they allowed you to still use raw types, and just let the compiler give you a warning when you write new code that uses raw types.
What is the relation between "ArrayList <Integer> al;" and "new ArrayList <Integer> ();"??
What do you mean? The one is a variable declaration and the other is a constructor call. It doesn't have anything to do with generics.