Generics Flashcards
(44 cards)
they add stability to your code by making more of your bugs detectable at compile time
generics
They provide a way for you to re-use the same code with different inputs
type parameters
Why use generics?
- stronger type checks at compile time
- elimination of casts
- enable programmers to implement generic algorithms
This is a generic class or interface that is parameterized over types
class name<T1, T2, …, Tn> { /* … */ }
generic type
You can replace the type arguments required to invoke the constructor of a generic class with an empty set of type arguments (<>) as long as the compiler can determine, or infer, the type arguments from the context. This pair of angle brackets, <>, is informally called the diamond.
Box<Integer> integerBox = new Box<>();</Integer>
The Diamond.
The name of a generic class or interface without any type arguments.
Raw Types
These are methods that introduce their own type parameters.
a type parameter before the return type of the
method declaration. This is needed even if the method is returning void.
Generic methods
methods are used to restrict the number of types of data for a class
Bounded Type
Parameters
a Java compiler’s ability to look at each method
invocation and corresponding declaration to determine the type
argument (or arguments) that make the invocation applicable.
Type inference
determines the types of the arguments and, if
available, the type that the result is being assigned, or returned.
tries to find the most specific type that
works with all of the arguments.
The inference algorithm
represents
an unknown type.
Wildcards ?
never used as a type argument for a generic method
invocation, a generic class instance creation, or a supertype.
wildcard
relax the restrictions on a variable.
restricts to its
sub-type.
Upper Bounded Wildcards.
List<? extends Number>
a list of unknown
type.
public static void printList(List<?> list) {
Unbounded Wildcards
<? super A>.
restricts the unknown type to be a specific type or a super-type
Lower Bounded Wildcards.
What does type erasure do?
- replace all type parameters in generic types with their bounds or object if the type parameters are unbounded.
- insert type casts if necessary to preserve type safety
- generate bridge methods to preserve polymorphism in extended generic types
It ensures that no new classes are created for parameterized types; consequently, generics incur no runtime overhead.
Type Erasure
Write a generic method to count the number of elements in a collection that have a specific property (for example, odd integers, prime numbers, palindromes).
public final class Algorithm {
public static <T> int countIf(Collection<T> c, UnaryPredicate<T> p) {</T></T></T>
int count = 0; for (T elem : c) if (p.test(elem)) \++count; return count; } }
Will the following class compile? If not, why?
public final class Algorithm {
public static <T> T max(T x, T y) {
return x > y ? x : y;
}
}</T>
No. The greater than (>) operator applies only to primitive numeric types.
Write a generic method to exchange the positions of two different elements in an array.
public final class Algorithm {
public static <T> void swap(T[] a, int i, int j) {
T temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}</T>
If the compiler erases all type parameters at compile time, why should you use generics?
You should use generics because:
The Java compiler enforces tighter type checks on generic code at compile time.
Generics support programming types as parameters.
Generics enable you to implement generic algorithms.
What is the following class converted to after type erasure?
public class Pair<K, V> {
public Pair(K key, V value) { this.key = key; this.value = value; } public K getKey(); { return key; } public V getValue(); { return value; } public void setKey(K key) { this.key = key; } public void setValue(V value) { this.value = value; } private K key; private V value; }
ublic class Pair {
public Pair(Object key, Object value) { this.key = key; this.value = value; } public Object getKey() { return key; } public Object getValue() { return value; } public void setKey(Object key) { this.key = key; } public void setValue(Object value) { this.value = value; } private Object key; private Object value; }
What is the following method converted to after type erasure?
public static <T extends Comparable<T>>
int findFirstGreaterThan(T[] at, T elem) {
// ...
}</T>
public static int findFirstGreaterThan(Comparable[] at, Comparable elem) {
// …
}
Will the following method compile? If not, why?
public static void print(List<? extends Number> list) {
for (Number n : list)
System.out.print(n + “ “);
System.out.println();
}
Yes