Great book written by Angelika Langer.
I had already read this book in the past and even spotted 4 bugs from the book.
http://prasanthaboutjava.blogspot.com/2008/03/4-things-i-donot-understand-in.html
I wanted to share few important quotations found from the book from page 1 - 100.
1) Java Generics are a language feature that allows for definition and use of generic types and methods.
2) The compiler translates generic and parameterized types by a technique called type erasure.
3) It is illegal to define generic type that are directly or indirectly derived from class Throwable. Consequently, no parameterized types appear anywhere in exception handling.
4) An instantiation of a generic type where all type arguments are concrete types rather than wildcards is called concrete parameterized type. Examples of concrete parameterized types are List
5) Parameterized types are not never covariant so
List<Object>
is not a supertype of
.
List<String>
6) Instantiations of different generic types for the same type argument can be compatible.
Example:
void printAll(Collection<Object> c) {
for (Object o : c)
System.out.println(o);
}
List<Object> list = new ArrayList<Object>();
printAll(list); // ok
7) Concrete parameterized types cannot be used in below situations
a) for creation of arrays
b) in exception handling
c) in a class literal
d) in an instanceof expression
Because generic type information will be erased at runtime.
8) Because parameterized type has no exact runtime type representation there is no class literal for concrete parameterized types.
9) A wildcard parameterized type cannot be not a supertype as it is of no use.
10) A place holder for a type argument is called a type parameter.
Example of a parameterized type:
interface Comparable<E> {
int compareTo(E other);
}
The identifier E is a type parameter. Each type parameter is replaced by a type argument when an instantiation of the generic type, such as
Comparable<Object>
or
Comparable<? extends String>
...
11) A type parameter with one or more bounds is called a bounded type parameter. The bounds restrict the set of types that can be used as type arguments and give access to the methods defined by the bounds.
Ex:
TreeMap<Key extends Comparable<Key>,Data>
Specification of a bound has two effects:
a) It gives access to the methods that the bound specifies. In the example, the bound
gives access to the compareTo method that we want to invoke in the implementation of our TreeMap class.
Comparable<Key>
b) Only types "within bounds" can be used for instantiation of the generic type.
12) Primitive types and array types are not permitted as type parameter bounds.
class X0 <T extends int> { ... } // error
class X1 <T extends Object[]> { ... } // error
13) Enum types, such as Thread.State are also permitted as type parameter bound. Thread.State is an example of a nested type used as type parameter bound. Non-static inner types are also permitted.
14) Parameterized types are permitted as type parameter bound, including concrete parameterized types such as List
parameterized type allows as type argument all types that belong to the type family that the wildcard denotes. The wildcard parameterized type bound gives only restricted access to fields and methods; the restrictions depend on the kind of wildcard.
15) A type parameter can be used as the bound of another type parameter.
Example (of a type parameter used as a type parameter bound):
class Triple<T> {
private T fst, snd, trd;
public <U extends T, V extends T, W extends T> Triple(U arg1, V arg2, W
arg3) {
fst = arg1;
snd = arg2;
trd = arg3;
}
}
16) At most one instantiation of the same generic type can appear in the list of bounds of a type parameter.
Example (of illegal use of two instantiations of the same generic type as bounds of a type parameter):
class ObjectStore<T extends Comparable<T> & Comparable<String>> { // error
private Set<T> theObjects = new TreeSet<T>();
...
public boolean equals(ObjectStore<String> other) {
if (theObjects.size() != other.size()) return false;
Iterator<T> iterThis = theObjects.iterator();
Iterator<String> iterOther = other.theObjects.iterator();
while (iterThis.hasNext() && iterOther.hasNext()) {
T t = iterThis.next();
String string = iterOther.next();
if (t.compareTo(string) != 0) return false;
}
return true;
}
}
17) A bound that is a class type gives access to all its public members except any constructors.
18) The declaration "
Enum<E extends Enum<E>>
" can be decyphered as: Enum is a generic type that can only
be instantiated for its subtypes, and those subtypes will inherit some useful methods, some of which take subtype specific arguments (or otherwise depend on the subtype).
19) We cannot create an object whose type is a type parameter because the compiler does not know how to create objects of an unknown type.
20) Type parameters can appear in throws clauses, but not in catch clauses.
21) Because a type parameter does not have a runtime type representation of its own there is no class literal for a type parameter.
22) The scope of a class's type parameter is the entire definition of the class, except any static members or static initializers of the class. This means that the type parameters cannot be used in the declaration of static fields or methods or in static nested types or static initializers.
No comments:
Post a Comment