Java
  Home arrow Java arrow Page 6 - Generics of Java 1.5 Tiger
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVA

Generics of Java 1.5 Tiger
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 15
    2005-05-26

    Table of Contents:
  • Generics of Java 1.5 Tiger
  • Using Type-Safe Maps
  • Iterating Over Parameterized Types
  • Accepting Parameterized Types as Arguments
  • Returning Parameterized Types
  • Checking for Lint
  • Generics and Type Conversions
  • Using Type Wildcards
  • Writing Generic Types
  • Restricting Type Parameters

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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.

    More Java Articles
    More By O'Reilly Media


       · List<Integer> ints = new ArrayList<Integer>();ints.add(1);ints.add(2);// This...
       · public static int biggest(Box<T> box1, Box<U> box2) { int box1Size =...
     

    Buy this book now. This article was taken from chapter two of Java 1.5 Tiger: A Developer's Notebook, written by Brett McLaughlin and David Flanagan (O'Reilly, 2004; ISBN: 0596007388). Check it out at your favorite bookstore. Buy this book now.

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






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
    Stay green...Green IT