Home arrow Java arrow Page 6 - 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 / 25
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

Generics of Java 1.5 Tiger - Checking for Lint
(Page 6 of 10 )

Several times in this chapter, you’ve heard about lint warnings, which sounds more like something you get out of a dryer than a compiler. These warnings are a new feature of Tiger, though, and important in figuring out how to really bulletproof your code.

How do I do that?

Take a simple piece of code that used a type that can be parameterized, but without type parameters:

  private List getList() {
    List list = new LinkedList();
    list.add(3);
    list.add("Blind");
    list.add("Mice");
    return list;
  }

If you compile this in Tiger, with the -source 1.5 flag, you’ll get this message:

  Note: GenericsTester.java uses unchecked or unsafe operations.
  Note: recompile with -Xlint:unchecked for details.

You can compile all
of the examples
for this book with
“-Xlint:unchecked”
by using the Ant
target “compile-
check”.

If you recompile with the suggested flag, you are telling the compile to
show lint warnings (-Xlint), and specifically to show those warnings that are unchecked. .

Here’s some sample output with these warnings turned on:

          [javac] code\src\com\oreilly\tiger\ch02\GenericsTester.java:63: warning:
          
[unchecked] unchecked call to add(E) as a member of
          the raw type java.util.List
  [javac]     list.add(3);
  [javac]         ^
  [javac] src\com\oreilly\tiger\ch02\GenericsTester.java:64: warning:
          [unchecked] unchecked call to add(E) as a member of
          the raw type java.util.List
  [javac] list.add("Blind");
  [javac]     ^
  [javac] src\com\oreilly\tiger\ch02\GenericsTester.java:65: warning:
          [unchecked] unchecked call to add(E) as a member of
          the raw type java.util.List
  [javac]     list.add("Mice");
  [javac]         ^
  [javac] 3 warnings

These warnings indicate that the compiler isn’t able to ensure that the values added to the list (named list in this case) are the intended type. That’s because list wasn’t parameterized.

You can get rid of these warnings by specifying a type in your List construction:

  private List getList() {
    List<Object> list = new LinkedList<Object>();
    list.add(3);
    list.add("Blind");
    list.add("Mice");
    return list;
  }

Autoboxing is
covered in
Chapter 4.

While this doesn’t do much for type-safety, it does take care of the warnings, as the types being added to list are all of type Object (the literal 3 is autoboxed to an Integer, which is of course an Object).

What about...

...annotations? For those of you who may be ahead on Tiger, there is an annotation, called SuppressWarnings, which allows you to keep these warnings from showing up in a compilation using -source 1.5. You can also just recompile under Java 1.4, although that’s obviously a pretty shortsighted solution. The best choice, if at all possible, is to parameterize your generic types, and enforce type-safety whenever possible.

Annotations are
covered in
Chapter 6.


blog comments powered by Disqus
JAVA ARTICLES

- 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 
Support 

Developer Shed Affiliates

 




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