Govt. Exams
Entrance Exams
Arrays of generic types are not allowed in Java due to type erasure and heap pollution prevention.
List list = new ArrayList();
list.add("Hello");
List raw = list; // Unchecked assignment
raw.add(123); // Adding Integer to raw type
String s = list.get(1);
Raw type assignment bypasses generics. Integer 123 is added to the list. When casting to String at get(1), ClassCastException occurs.
ArrayList<Integer> is a subtype of List<Integer> because ArrayList is a subclass of List with the same type parameter.
public T getMax(T a, T b) {
return a.compareTo(b) > 0 ? a : b;
}
What is the benefit of this recursive bound ?
Recursive bound <T extends Comparable<T>> ensures that T implements Comparable interface specifically for comparing with its own type, providing type safety.
List list = new ArrayList();
list.add(123); // Adding Integer
String s = (String) list.get(0);
Raw type List accepts any object. At runtime, the Integer 123 cannot be cast to String, causing ClassCastException.
PECS principle: use 'extends' when reading from a collection (source), use 'super' when writing to it (dest). Option A reads from source and writes to dest correctly.