18.2. Motivation for Generic Methods
Overloaded methods are often used to perform similar operations on different types of data. To motivate generic methods, let’s begin with an example (Fig. 18.1) that contains three overloaded printArray methods (lines 7–14, lines 17–24 and lines 27–34). These methods print the string representations of the elements of an Integer array, a Double array and a Character array, respectively. Note that we could have used arrays of primitive types int, double and char in this example. We chose to use arrays of type Integer, Double and Character to set up our generic method example, because only reference types can be used with generic methods and classes.
Figure 18.1. Printing array elements using overloaded methods. ...
Get Java™ How to Program, Seventh Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.