Deitel & Associates, Inc. Logo

Back to www.deitel.com
digg.png delicious.png blinkit.png furl.png
Java How to Program, 6/e

ISBN:
0-13-148398-6
© 2005
pages: 1576
Buy the Book!
Amazon logo
InformIT logo

Part 1: Introduction to Java Generics

This is the first in a series of three articles that shows how to declare and use generic methods in Java Standard Edition 5.0 (Java SE 5.0). In this article, we introduce the concept of generics. In Part 2, we present an example using overloaded methods to motivate the need for generic methods. In Part 3, we reimplement the overloaded methods from the example in Part 2 using a single generic method. These articles are intended for students who are already familiar with Java and for Java developers.

[Note: This series of three articles (Part 1, Part 2, Part 3) is an excerpt (Section 18.1) of Chapter 18, Generics, from our textbook Java How to Program, 6/e. These articles may refer to other chapters or sections of the book that are not included here. Permission Information: Deitel, Harvey M. and Paul J., JAVA HOW TO PROGRAM, ©2005, pp.870-876. Electronically reproduced by permission of Pearson Education, Inc., Upper Saddle River, New Jersey.]

It would be nice if we could write a single sort method that could sort the elements in an Integer array, a String array or an array of any type that supports ordering (i.e., its elements can be compared). It would also be nice if we could write a single Stack class that could be used as a Stack of integers, a Stack of floating-point numbers, a Stack of Strings or a Stack of any other type. It would be even nicer if we could detect type mismatches at compile time—known as compile-time type safety. For example, if a Stack stores only integers, attempting to push a String on to that Stack should issue a compile-time error.

    This chapter discusses one of J2SE 5.0’s new features—generics—which provides the means to create the general models mentioned above. Generic methods and generic classes enable programmers to specify, with a single method declaration, a set of related methods or, with a single class declaration, a set of related types, respectively. Generics also provide compile-time type safety that allows programmers to catch invalid types at compile time.

    We might write a generic method for sorting an array of objects, then invoke the generic method with Integer arrays, Double arrays, String arrays and so on, to sort the array elements. The compiler could perform type checking to ensure that the array passed to the sorting method contains same type elements. We might write a single generic Stack class that manipulates a stack of objects, then instantiate Stack objects for a stack of Integers, a stack of Doubles, a stack of Strings and so on. The compiler could perform type checking to ensure that the Stack stores elements of the same type.

Software Engineering Observation 18.1
Generic methods and classes are among Java’s most powerful capabilities for software reuse with compile-time type safety.

    This chapter presents generic method and generic class examples. It also considers the relationships between generics and other Java features, such as overloading and inheritance. Chapter 19, Collections, presents an in-depth treatment of the Java Collections Framework’s generic methods and classes. A collection is a data structure that maintains references to many objects. The Java Collections Framework uses generics to allow programmers to specify the exact types of objects that a particular collection will store in a program.

Other Tutorials in This Series
Part 2: Motivation for Generic Methods in Java
Part 3: Generic Methods—Implementation and Compile-Time Translation

Tutorial Index