// To retrieve an element, an explicit type cast is needed
// because the collection stores only Object.
String str = (String) itr.next(); // explicit cast needed here.
System.out.println(str + " is " + str.length() + " chars long.");
}
}
}
Prior to generics, all collections stored references of type Object. This allowed any type
of reference to be stored in the collection. The preceding program uses this feature to store
references to objects of type String in list, but any type of reference could have been stored.
Unfortunately, the fact that a pre-generics collection stored Object references could easily
lead to errors. First, it required that you, rather than the compiler, ensure that only objects of
the proper type be stored in a specific collection. For example, in the preceding example, list
is clearly intended to store Strings, but there is nothing that actually prevents another type
of reference from being added to the collection. For example, the compiler will find nothing
wrong with this line of code:
list.add(new Integer(100));
Because list stores Object references, it can store a reference to Integer as well as it can
store a reference to String. However, if you intended list to hold only strings, then the preceding
statement would corrupt the collection. Again, the compiler had no way to know that the
preceding statement is invalid.
The second problem with pre-generics collections is that when you retrieve a reference
from the collection, you must manually cast that reference into the proper type. This is why
the preceding program casts the reference returned by next( ) into String. Prior to generics,
collections simply stored Object references. Thus, the cast was necessary when retrieving
objects from a collection.
Aside from the inconvenience of always having to cast a retrieved reference into
its proper type, this lack of type safety often led to a rather serious, but surprisingly
easy-to-create, error. Because Object can be cast into any type of object, it was possible to
cast a reference obtained from a collection into the wrong type. For example, if the following
statement were added to the preceding example, it would still compile without error, but
generate a run-time exception when executed:
Integer i = (Integer) itr.next();
Recall that the preceding example stored only references to instances of type String in list.
Thus, when this statement attempts to cast a String into an Integer, an invalid cast exception
results! Because this happens at run time, this is a very serious error.
The addition of generics fundamentally improves the usability and safety of collections
because it
· Ensures that only references to objects of the proper type can actually be stored in
a collection. Thus, a collection will always contain references of a known type.
· Eliminates the need to cast a reference retrieved from a collection. Instead, a reference
retrieved from a collection is automatically cast into the proper type. This prevents
run-time errors due to invalid casts and avoids an entire category of errors.
Search WWH :
Custom Search
Previous Page
Java SE 6 Topic Index
Next Page
Java SE 6 Bookmarks
Home