Java
  Home arrow Java arrow Page 9 - 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 
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: 4 stars4 stars4 stars4 stars4 stars / 16
    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 - Writing Generic Types


    (Page 9 of 10 )

    With an arsenal of generic terminology under your belt, you’re probably wondering about writing your own generic types. I’m wondering about it, too, so I figure it’s worth covering. They’re actually pretty simple to write, and you’ve already got the tools from earlier labs.

    How do I do that?

    If you need to define some sort of collection, or container, or other custom object that deals directly with another type, generics add a ton of options to your programming toolkit. For example, Example 2-2 is a basic container structure useful mostly for illustrating important generic concepts.

    You can use
    anything you want for the type
    parameter, although a single
    letter is most
    common.

    Example 2-2. A basic generic type

      package com.oreilly.tiger.ch02;
      import java.util.ArrayList;
      import java.util.List;
      public class Box<T> {
        protected List<T> contents;
        public Box() {
          contents = new ArrayList<T>();
       
    }
       
    public int getSize()
          return contents.size();
        }
        public boolean isEmpty() {
          return (contents.size() == 0);
        }
        public void add(T o) {
          contents.add(o);
        }
        public T grab() {
          if (!isEmpty()) {
            return contents.remove(0);
          } else
            return null;
        }
      }

    Just as you’ve seen in Tiger’s pre-defined generic types, a single letter is used as the representative for a type parameter.

    You create a new instance of this type exactly as you might expect:

      Box<String> box = new Box<String>();

    This effectively replaces all the occurrences of T with String for that specific instance, and suddenly you’ve got yourself a String Box, so to speak.

    What about…

    …static variables? Static variables are shared between object instances, but parameterization occurs on a per-instance basis. So you could feasibly have a Box<Integer>,a  Box<String>, and a Box<List<Float>>, all with a shared static variable. That variable, then, cannot make assumptions about the typing of any particular instance, as they may be different. It also cannot use a parameterized type—so the following is illegal:

      private static List<T> staticList = new ArrayList<T>();

    You can, however, use static methods that themselves have parameterized types:

      public static int biggest(Box<T> box1, Box<U> box2) {
        int box1Size = box1.getSize();
        int box2Size = box2.getSize();
        return Math.max(box1Size, box2Size);
      }

    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-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    Stay green...Green IT