Testing the Generic printArray Method

As in Fig. 20.1, the program in Fig. 20.3 begins by declaring and initializing six-element Integer array integerArray (line 9), seven-element Double array doubleArray (line 10) and five-element Character array characterArray (line 11). Then each array is output by calling printArray (lines 14, 16 and 18)—once with argument integerArray, once with argument doubleArray and once with argument characterArray.

When the compiler encounters line 14, it first determines argument integerArray’s type (i.e., Integer[]) and attempts to locate a method named printArray that specifies a single Integer[] parameter. There’s no such method in this example. Next, the compiler determines whether there’s a generic method named ...

Get Java™ How To Program (Early Objects), Tenth 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.