|
Generics -- also commonly known as parameterized types -- have been used in other computing languages for years. And now, due to popular demand, generics is slated to be added to the Java language with the 1.5 release. The Problem with ListsWith generics, types that contain data (such as lists) are not defined to operate over a specific type of data; instead, they operate over a homogeneous set, where the set type is defined at declaration. How exactly does that help the Java developer?
The best way to understand what generics can do for you is to study some Java code that would benefit from generics. The sample below, written to the current Java language specification, contains a list of String object elements and a list of Integer object elements (not the primitive type). Because both elements are subclasses of the Below are some examples of such errors. In the first example (lines 13 to 20), you might believe you're working with a list of Integer objects, when in reality it's a list of Strings. In the second example (lines 11 and 22 to 27), you might think you're working with a homogenous set of String, but this is a heterogeneous set of both String and Integer elements. So unless you create a new list subclass for every element type (which would undermine the advantages of OO reuse), there's no way to statically constrain the list to a set of homogeneous elements. And in this simple example, the errors are fairly easy to catch. In a bigger program, you'd have even bigger problems.
The Generics AdvantageWith generics, you achieve polymorphic behavior similar to the example above, but with strong static type-checking; the compiler knows that the two lists are different because they contain different elements, and these lists are guaranteed to contain only a homogeneous set of elements. The sample code below is a translation of the previous example, using generics this time. As you can see from comments in the code, all the errors are caught at compile time. Don't worry about the syntax for now -- we'll cover that shortly. In comparing the two examples, you should notice that additional type information is included in the generics code, which directs the compiler as to what type each container should contain.
With these examples, you can understand why generics is among the most-requested additions to the Java language. Here are a few of the benefits:
Basic Syntax OverviewSo far, you've only seen the genericized container classes provided to us. But you can also declare your own generic interfaces, classes, and methods. The following table illustrates the syntax you would use.
The following example uses similar syntax in code: MyInterface Getting Started with Generics
For more information on generics, you can refer to the specification and to the JSR 14. Developers can start writing generics code today using a prototype implementation of the Java compiler, which supports generics (as described in the draft specification). The prototype includes: the source written in the extended language, a
See AlsoAbout the AuthorPaul Mingardi is a Market Development Engineer at Sun Microsystems, working on technology adoption for partners in health care, banking, and finance. He also works with independent software vendors, to help them become successful using Java and other Sun technologies. Paul is a Sun Certified Enterprise Architect and holds a total of seven certifications. | ||||||||||||||||||||
Oracle is reviewing the Sun product roadmap and will provide guidance to customers in accordance with Oracle's standard product communication policies. Any resulting features and timing of release of such features as determined by Oracle's review of roadmaps, are at the sole discretion of Oracle. All product roadmap information, whether communicated by Sun Microsystems or by Oracle, does not represent a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. It is intended for information purposes only, and may not be incorporated into any contract.
|
| ||||||||||||