Defining Simple Generics
Here is a small excerpt from the definitions of the interfacesList and Iterator in package
java.util:
public interface List
{
void add(E x);
Iterator iterator();
}
public interface Iterator
{
E next();
boolean hasNext();
}
Here is a small excerpt from the definitions of the interfacesList and Iterator in package
java.util:
public interface List
{
void add(E x);
Iterator
}
public interface Iterator
{
E next();
boolean hasNext();
}
- This should all be familiar, except for the stuff in angle brackets.
- Those are the declarations of the formal type parameters of the interfaces List and Iterator.
- Type parameter can be used throughout the generic declaration, pretty much where
you would use ordinary types (though there are some important restrictions; - In the introduction, we saw invocations of the generic type declaration List, such as List
. - In the invocation (usually called a parameterized type), all occurrences of the formal type parameter (E in this case) are replaced by the actual type argument (in this case, Integer).
0 comments:
Post a Comment