Using Generics

This lesson lists caveats one should be aware of when working with generics in Java.

We'll cover the following...

Notes on using Generics

  1. Don't use raw types as they can cause code to fail at runtime with casting exceptions. Using raw types loses type safety and expressiveness advantages of generics. You can put any element into a collection with a raw type, easily corrupting the collection’s type invariant.

  2. Eliminate checked warnings. If you eliminate all warnings, you are assured that your code is typesafe, which is a very good thing. It means that you won’t get a ClassCastException at runtime, and it increases your confidence that your program is behaving as you intended.

  3. Always use the SuppressWarnings ...