Writing Generic Types and Methods. This week David details how to write your own generic types and generic methods, and concludes with a tour of important generic types in the core Java API.
Tutorial Details:
Creating a simple generic type is straightforward. First, declare your type variables by enclosing a comma-separated list of their names within angle brackets after the name of the class or interface. You can use those type variables anywhere a type is required in any instance fields or methods of the class. Remember, though, that type variables exist only at compile time, so you can\'t use a type variable with the runtime operators instanceof and new.
We begin this section with a simple generic type, which we will subsequently refine. This code defines a Tree data structure that uses the type variable V to represent the type of the value held in each node of the tree:
Read
Tutorial at: Click here to view the tutorial
Rate Tutorial: Generic Types, Part 2
View Tutorial: Generic Types, Part 2
Related
Tutorials:
XML JavaBeans, Part 2 - JavaWorld March 1999
XML JavaBeans, Part 2 - JavaWorld March 1999 |
XML JavaBeans, Part 3 - JavaWorld July
1999
XML JavaBeans, Part 3 - JavaWorld July
1999 |
Smart object-management saves the
day - JavaWorld November 1999
Smart object-management saves the
day - JavaWorld November 1999 |
Java performance programming,
Part 2: The cost of casting - JavaWorld December 1999
Java performance programming,
Part 2: The cost of casting - JavaWorld December 1999 |
Java performance programming,
Part 3: Managing collections - JavaWorld February 2000
Java performance programming,
Part 3: Managing collections - JavaWorld February 2000 |
Behold the power
of parametric
polymorphism - JavaWorld February
2000
Behold the power
of parametric
polymorphism - JavaWorld February
2000 |
Build an object database, Part 2: Object storage
backend - JavaWorld April 2000
Build an object database, Part 2: Object storage
backend - JavaWorld April 2000 |
Add MP3 capabilities to Java Sound with SPI - JavaWorld November
2000
Add MP3 capabilities to Java Sound with SPI - JavaWorld November
2000 |
Reveal the magic
behind subtype polymorphism - JavaWorld April
2001
Reveal the magic
behind subtype polymorphism - JavaWorld April
2001 |
Language improvements
and models make
great Java - JavaWorld
Language improvements
and models make
great Java - JavaWorld |
Object-oriented
language basics, Part
7
Object-oriented
language basics, Part
7 |
Boost Struts with
Boost Struts with XSLT and XML |
Check out three
collections libraries
Check out three
collections libraries |
Unwrap the package statement's
potential
Unwrap the package statement's
potential |
Beware the dangers of
generic Exceptions
Beware the dangers of
generic Exceptions |
Good, but
obsolete
Good, but
obsolete |
Taming Tiger
Taming Tiger, Part 2
Understanding generics
Welcome to the second part of this three-part series on Sun Microsystems' latest release of the Java 2 Platform, Standard Edition (J2SE). To refresh your memory, Part 1 was a quick introduction to J2SE 1.5 |
A Generic MVC Model in Java
A Generic MVC Model in Java
Model-View-Controller (MVC) is a widely used design pattern, especially popular in graphical user interface (GUI) programming. JDK 1.5 introduces parameterized types, or generics. Combining the two allows for a generic imple |
The Introduction to generic types in JDK 5.0
This tutorial introduces generic types, a new feature in JDK 5.0 that lets you define classes with abstract type parameters that you specify at instantiation time. Generics increase the type safety and maintainability of large programs. Follow along with |
Java theory and practice: Generics gotchas
Generic types, added in JDK 5.0, are a significant enhancement to type safety in the Java language. However, some aspects of generics may seem confusing, or even downright bizarre, to first-time users. In this month's Java theory and practice, Brian Goetz |
|
|
|