Author: Nesha Mason
-
Implications for Non-Reifiable Variable Arity Parameter – Generics
•
Implications for Non-Reifiable Variable Arity Parameter Because variable arity parameters are treated as arrays, generics have implications for non-reifiable variable arity parameters (T … varargs). Most of the workarounds for arrays are not applicable, as array creation is implicit for variable arity parameters. In a method declaration with a non-reifiable…
-
Implications for the instanceof operator – Generics
•
Implications for the instanceof operator Although the discussion here is about the instanceof type comparison operator, it applies equally to the instanceof pattern match operator. At (1) below, we want to determine whether the object referenced by obj is an instance of the concrete parameterized type MyStack<Integer>—that is, whether it…
-
Primitive Values and ArrayLists – Collections, Part I: ArrayList
•
Primitive Values and ArrayLists Since primitive values cannot be stored in an ArrayList<E>, we can use the wrapper classes to box such values first. In the following code, we create a list of Integer in which the int values are autoboxed in Integer objects and then added to the list.…
-
Implementing a Simplified Generic Stack – Generics
•
11.8 Implementing a Simplified Generic Stack The Node<E> class from Example 11.2, p. 568, can be used to implement linked data structures. Example 11.10 is an implementation of a simplified generic stack using the Node<E> class. The emphasis is not on how to develop a full-blown, industrial-strength implementation, but on…
-
Type Erasure – Generics
•
11.11 Type Erasure Understanding translation by type erasure aids in understanding the restrictions and limitations that arise when using generics in Java. Although the compiler generates generic-free bytecode, we can view the process as a source-to-source translation that generates non-generic code from generic code. The translated code has no information…
-
Capture Conversion – Generics
•
Capture Conversion Consider the following non-generic method which does not compile: Click here to view code image static void fillWithFirstV1(List<?> list) { Object firstElement = list.get(0); // (1) for (int i = 1; i < list.size(); i++) list.set(i, firstElement); // (2) Compile-time error } The method should fill any list…
-
Declaring References and Constructing ArrayLists – Collections, Part I: ArrayList
•
12.2 Declaring References and Constructing ArrayLists In the discussion that follows, we assume that any class or interface used from the java.util package has been imported with an appropriate import statement. The code below illustrates how we can create an empty ArrayList of a specific element type, and assign its…
-
Bridge Methods – Generics
•
Bridge Methods Bridge methods are inserted in subclasses by the compiler to ensure that overriding of methods works correctly. The canonical example is the implementation of the Comparable interface. The post-erasure code of the class CmpNode<E> from Example 11.8 is shown below. A second compareTo() method has been inserted by…
-
Genericity and Inherited Methods – Generics
•
Genericity and Inherited Methods The subsignature requirement for overriding means that the signature of the subtype method must be the same as that of the supertype method, or it must be the same as the erasure of the signature of the supertype method. Note the implication of the last sentence:…
-
Other Implications – Generics
•
Other Implications Enum Types Because of the way enum types are implemented using the java.lang.Enum class, we cannot declare a generic enum type: Click here to view code image enum CoinToss<C> { HEAD, TAIL; } // Compile-time error! An enum type can implement a parameterized interface, just like a non-generic…