J.E.D.I.
13  An Introduction to Generics
13.1  Objectives
Javas latest release provides the biggest leap forward in Java programming compared to its other versions. It includes significant extensions to the source language syntax. The
most visible of which is the addition of generic types.
This module introduces you to the basic concepts related to Java generic types. After completing this lesson, you should be able to:
1. Enumerate the benefits of generic types 2. Declare a generic class
3. Use constrained generics 4. Declare generic methods
5. Use Java collections with generic types
13.2  Why Generics?
One  of  the  most  significant   causes of  bugs  in   the  Java  programming   language   is  the need to continually typecast or downcast expressions to more specific data types than
their static types. For example, an ArrayList object allows us to add any reference type object to the list but when we retrieve these elements, we need to typecast the objects
to a specific reference type appropriate for our needs. Downcasting is a potential hotspot for ClassCastException. It also makes our codes wordier, thus, less readable. Moreover,
downcasting also effectively destroys the benefits of a strongly typed language since it nullifies the safety that accompanies built-in type checking.
The main goal of adding generics to Java is to solve this problem. Generic types allow a single class to work with a wide variety of types. It is a natural way of eliminating the
need for casting.
Lets   first   consider   an  ArrayList  object   and   see   how   generic   types   would   help   in improving  our code. As you already know, an  ArrayList  object has the ability  to store
elements of any reference type to this list. An  ArrayList  instance, however, has always forced   us   to   downcast   the   objects   we   retrieve   out   of   the   list.   Consider   the   following
statement:
String myString = String myArrayList.get0; The generic version of the  ArrayList  class is designed to work natively with any type of
class. At the same, it also preserves the benefits of type checking. We can do away with the need of having to typecast the element we get from the list and have the following
statement instead of the previous one:
String myString = myArrayList.get0; Although   downcasting   was   already   removed, this   doesnt  mean  that   you   could  assign
anything to the return value of the get method and do away with typecasting altogether. If you assign anything else besides a String to the output of the get method, you would
Introduction to Programming II Page 179
J.E.D.I.
encounter a compile time type mismatch such as this message: found: java.lang.String
required: java.lang.Integer Integer data = myArrayList.get0;
For you to just have an idea how generic types are used before digging into more details, consider the following code fragment:
ArrayList String genArrList = new ArrayList String; genArrList.addA generic string;
String myString = genArrList.get0; JoptionPane.showMessageDialogthis, myString;
Browsing through the statements, you probably observed the word String  appearing immediately after the reference data type ArrayList. You can interpret the first statement
as instantiating a generic version of the ArrayList class and this generic version contains objects of type String. genArrList  is bound to  String  type. Hence, binding an Integer or
some other non-String type to the result of the get function would be illegal. The next statement is illegal.
int myInt = genArrList.get;
13.3  Declaring a Generic Class