Home arrow Java arrow Page 4 - Generics of Java 1.5 Tiger

Generics of Java 1.5 Tiger

The generics feature of Tiger brings greater type safety to Java, allowing developers to do many things they could not do before. Generics bear on a number of other features specific to Tiger. This article introduces you to generics, and what they can do. It is excerpted from chapter two of Java 1.5 Tiger: A Developer's Notebook, written by Brett McLaughlin and David Flanagan (O'Reilly, 2004; ISBN: 0596007388).

Author Info:
By: O'Reilly Media
Rating: 4 stars4 stars4 stars4 stars4 stars / 25
May 26, 2005
  1. · Generics of Java 1.5 Tiger
  2. · Using Type-Safe Maps
  3. · Iterating Over Parameterized Types
  4. · Accepting Parameterized Types as Arguments
  5. · Returning Parameterized Types
  6. · Checking for Lint
  7. · Generics and Type Conversions
  8. · Using Type Wildcards
  9. · Writing Generic Types
  10. · Restricting Type Parameters

print this article

Generics of Java 1.5 Tiger - Accepting Parameterized Types as Arguments
(Page 4 of 10 )

So far, all of this parameterization has occurred in the same code block. However, thatís unrealistic, and youíll quickly want to write methods that take advantage of parameterized types. This is where generics start to really become powerful. First, you need to understand how a method can tell the compiler that it only accepts a specific parameterization of a generic type.

How do I do that?

Just use the same syntax youíve been using (and which should be getting oddly comfortable by this point) in your argument list:

  private void printListOfStrings(List<String> list, PrintStream out)
    throws IOException {
    for (Iterator<String> i = list.iterator(); i.hasNext(); ) {

This allows your method body to act on that parameterization, avoiding class casts and the like. In this example, itís possible to parameterize the Iterator as well, because the compiler ensures that only List<String> is passed into the method. Any other List types are refused (at compile-time).

What about...

...trying to pass in a plain old List, without any parameterization, even if it has only Strings in it? This actually will work, with the caveat that youíre left to your own devices in ensuring that the List has in it what itís supposed to. If not, youíll get more ClassCastExceptions than you can shake a stick at, all at runtime. In either case, youíll get lint warnings, which are described in ďChecking for Lint,Ē later in this chapter.

blog comments powered by Disqus

- Java Too Insecure, Says Microsoft Researcher
- Google Beats Oracle in Java Ruling
- Deploying Multiple Java Applets as One
- Deploying Java Applets
- Understanding Deployment Frameworks
- Database Programming in Java Using JDBC
- Extension Interfaces and SAX
- Entities, Handlers and SAX
- Advanced SAX
- Conversions and Java Print Streams
- Formatters and Java Print Streams
- Java Print Streams
- Wildcards, Arrays, and Generics in Java
- Wildcards and Generic Methods in Java
- Finishing the Project: Java Web Development ...

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2019 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials