Java Tutorials - Herong's Tutorial Examples - Version 7.03, by Dr. Herong Yang
Using a Generic Class
This section describes 4 basic areas that require attention with using a generic class: calling the constructor, declaring a reference variable, passing an argument to methods and handling the returning reference from methods.
When using a generic class, you need to pay attention to 4 basic areas:
1. Calling the constructor of a generic class - When calling a constructor of a generic class, you need to provide a specific type to the type parameter enclosed in angle brackets after the class name. Examples are listed below:
new java.util.Stack<double[]>(); new java.util.ArrayList<String>(); new java.util.Vector<Number>(200);
2. Declaring a variable for a generic class type - When declaring a variable for a generic class type, you need to provide a specific type to the type parameter enclosed in angle brackets after the class name. Examples are listed below:
java.util.Stack<double[]> stackForDoubleArray; java.util.ArrayList<String> arrayListForString; java.util.Vector<Number> vectorForNumber;
3. Passing an argument to a method that uses the type parameter - When calling a method in a generic class that uses a type parameter as an argument, you need to pass a reference that is compatible with the specific type provided to the generic class. Examples are listed below for the method "boolean add(E e)" in the class "java.util.Vector<E>":
java.util.Vector<Number> verctorForNumber; ... vectorForNumber.add(new Double(9.99)); // allowed - Double is compatible with Number vectorForNumber.add(new String("9.99")); // not allowed - String is not compatible with Number
4. Receiving return reference from a method that uses the type parameter - When receiving a reference from a method in a generic class that uses a type parameter as the return value, you need to handle it as the specific type provided to the generic class. Examples are listed below for the method "E firstElement()" in the class "java.util.Vector<E>":
java.util.Vector<Number> verctorForNumber; ... Object firstTry = vectorForNumber.firstElement(); // allowed - Return type Number can be up casted to Object Double secondTry = vectorForNumber.firstElement(); // not allowed - Double is not compatible with Number
Last update: 2014.
Table of Contents
Execution Process, Entry Point, Input and Output
Primitive Data Types and Literals
Bits, Bytes, Bitwise and Shift Operations
Managing Bit Strings in Byte Arrays
Reference Data Types and Variables
StringBuffer - The String Buffer Class
System Properties and Runtime Object Methods
►Generic Classes and Parameterized Types
Using a Generic Class - Example
Creating a Generic Class - Example
Raw Type, Generic Type and Parameterized Type
Parameterized Type and Subtyping
Wildcard Parameterized Type Test
Wildcard Parameterized Subtyping
Wildcard Parameterized Subtyping Example
Generic Methods and Type Inference
Lambda Expressions and Method References
Execution Threads and Multi-Threading Java Programs
ThreadGroup Class and "system" ThreadGroup Tree
Synchronization Technique and Synchronized Code Blocks
Deadlock Condition Example Programs