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

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 / 17
May 26, 2005
TABLE OF CONTENTS:
  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
SEARCH DEVARTICLES

TOOLS YOU CAN USE

advertisement
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(); ) {
      out.println(i.next());
    }
  }

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 ARTICLES

- 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 ...
- Generics and Limitations in Java
- Getting Started with Java Web Development in...

Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 



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