Loading…
Loading…
Topic
Articles and problems tagged Generics.
Generic wildcards for the OCP 21 exam: why List<String> isn't a List<Object>, unbounded ?, upper-bounded ? extends and lower-bounded ? super, what you can read and add through each, PECS, List<?> versus var, and the tricky declarations the exam uses.
Generics for the OCP 21 exam: declaring generic classes, interfaces and records, type parameter naming, type erasure and its limits, overloading and overriding with generic types, generic methods and explicit type arguments, shadowed type parameters, and bounded type parameters.
How Java packages and imports work for the OCP 21 exam: wildcards, java.lang, redundant imports, Date naming conflicts, fully qualified names, javac -d, the three class path options, JAR files, element order in a file and Java 25 module imports.
Learn what reflection can reveal about Java generics despite erasure: the generic Class<T> class, matching Class<T> parameters, the Type interface with TypeVariable, WildcardType, ParameterizedType and GenericArrayType, and type literals (super type tokens) for generic types.
The complete list of Java generics restrictions and their workarounds: no primitives, instanceof only on raw types, no generic arrays, @SafeVarargs, new T() and new T[], static context, generic exceptions, sneaky throws, erasure clashes and record pattern exhaustiveness.
Master Java wildcards: why List<Manager> is not a List<Employee>, how arrays differ, raw-type pitfalls, ? extends for reading, ? super for writing, the PECS rule, Comparable<? super T>, unbounded wildcards and wildcard capture with helper methods.
Understand how Java generics really work: type erasure to raw types and first bounds, the casts the compiler inserts, how generic methods are erased, bridge methods for overriding and covariant returns, calling legacy raw-type code, and generic record patterns.
Learn how to restrict Java type variables with bounds: why <T extends Comparable<T>> lets you call compareTo, why extends is used for interfaces, multiple bounds with &, the class-first rule, bounded generic classes, and the LocalDate surprise.
Learn why Java generics exist and how to use them: type parameters versus Object and casts, defining a generic class like Pair<T>, naming conventions, the diamond operator, generic methods, type inference and how to decode inference errors.
Finish Java reflection: create arrays of any type with java.lang.reflect.Array, grow arrays generically like Arrays.copyOf, call methods with Method.invoke, invoke constructors with arguments, handle InvocationTargetException, and compare reflection with method handles and lambdas.
Use Java's ArrayList, the resizable array: declaring ArrayList<Employee> with var and the diamond operator, add, get, set, insert and remove, size versus capacity, ensureCapacity and trimToSize, for-each loops, converting to arrays, Java 21 getFirst/getLast, and raw-type compatibility.