Java Reference
In-Depth Information
class Employee
private String name;
Employee(String name)
{ = name;
String getName()
return name;
class TypeSafety
public static void main(String[] args)
List<Employee> employees = new ArrayList<Employee>();
employees.add(new Employee("John Doe"));
employees.add(new Employee("Jane Smith"));
employees.add("Jack Frost");
Iterator<Employee> iter = employees.iterator();
while (iter.hasNext())
Employee emp =;
Listing 3-51 ' s refactored main() method illustrates the central feature of generics,
whichisthe parameterized type (aclassorinterfacenamefollowedbyananglebracket-
delimited type list identifying what kinds of objects are legal in that context).
For example, List<Employee> indicates only Employee objects can be stored
in the List . As shown, the <Employee> designation can be repeated with Ar-
rayList , as in Arraylist<Employee> , which is the collection implementation
that stores the Employee s. Because the compiler can figure out this type argument
Search WWH ::

Custom Search