|
Angelika Langer - Training & Consulting
|
|
|
|
HOME
| COURSES
| TALKS
| ARTICLES
| GENERICS
| LAMBDAS
| IOSTREAMS
| ABOUT
| CONTACT
|
|
|
|
Books
|
Java Generics FAQs - Generic Methods
|
This is a collection of answers to frequently asked questions
(FAQs) about Java Generics, a new language feature added to the Java programming
language in version 5.0 of the Java Standard Edition (J2SE 5.0).
If you want to provide feedback or have any questions regarding Java
generics, to which you cannot find an answer in this document, feel free
to send me
EMAIL
or use the
GENERICS FAQ
form.
A printable version of the FAQ documents is available in PDF
format (4.5MB).
|
Java Generics FAQs - Generic Methods
Generic
Methods
© Copyright 2004-2022 by Angelika Langer.
All Rights Reserved.
Generic Methods
What
is a generic method?
A method with type parameters.
|
Not only types can be generic, but methods can be generic,
too. Static and non-static methods as well as constructors can have type
parameters. The syntax for declaration of the formal type parameters is
similar to the syntax for generic types. The type parameter section is
delimited by angle brackets and appears before the method's return type.
Its syntax and meaning is identical to the type parameter list of a generic
type.
Here is the example of a generic
max
method that computes the
greatest value in a collection of elements of an unknown type
A
.
Example (of a generic method):
class Collections {
public static
<A extends Comparable<A>>
A
max(Collection<
A
> xs)
{
Iterator<
A
>
xi = xs.iterator();
A
w = xi.next();
while (xi.hasNext()) {
A
x = xi.next();
if (w.compareTo(x) < 0) w = x;
}
return w;
}
}
The
max
method has one type parameter, named
A
.
It is a place holder for the element type of the collection that the method
works on. The type parameter has a bound; it must be a type
A
that is a subtype of
Comparable<A>
, i.e., a type that can be
compared to elements of itself. |
LINK TO THIS
|
GenericMethods.FAQ001
|
REFERENCES
|
What
is a generic type?
How
do I define a generic type?
What
is a type parameter?
What
is a bounded type parameter?
|
How
do I invoke a generic method?
Usually by calling it. Type arguments
for generic methods need not be provided explicitly; they are almost
always automatically inferred.
|
Generic methods are invoked like regular non-generic methods.
The type parameters are inferred from the invocation context.
Example (of invocation of a generic method; taken from the preceding
item):
class
Collections {
public static <A extends Comparable<A>>
A max (Collection<A> xs) {
Iterator<A> xi = xs.iterator();
A w = xi.
next();
while (xi.hasNext()) {
A x = xi.next();
if (w.compareTo(x) < 0) w = x;
}
return w;
}
}
final class Test {
public static void main (String[ ] args) {
LinkedList<Long> list = new LinkedList<Long>();
list.add(0L);
list.add(1L);
Long y =
Collections.max(list)
;
}
}
In our example, the compiler would automatically invoke an instantiation
of the
max
method with the type argument
Long
, that is,
the formal type parameter
A
is replaced by type
Long
.
Note that we do not have to explicitly specify the type argument.
The compiler automatically infers the type argument by taking a look at
the type of the arguments provided to the method invocation. The compiler
finds that a
Collection<A>
is asked for and that a
LinkedList<Long>
is
provided. From this information the compiler concludes at compile
time that
A
must be replaced by
Long
. |
LINK TO THIS
|
GenericMethods.FAQ002
|
REFERENCES
|
What
is type argument inference?
What
explicit type argument specification?
What
happens if a type parameter does not appear in the method parameter list?
Why
doesn't type argument inference fail when I provide inconsistent method
arguments?
|
CONTENT
PREVIOUS
NEXT
INDEX
|
|
|
© Copyright 1995-2022
by
Angelika Langer. All Rights Reserved.
URL:
<
http://www.AngelikaLanger.com/GenericsFAQ/FAQSections/ParameterizedMethods.html>
last update: 14 Aug 2018
|