Java Tutorials - Herong's Tutorial Examples - Version 7.03, by Dr. Herong Yang
Using a Generic Class - Example
This section provides a tutorial example on using a generic class in 4 areas: calling the constructor, declaring a reference variable, passing an argument to methods and handling the returning reference from methods.
Let's see a tutorial example program that uses generic classes provided as part of JDK:
/* UsingGenericClass.java - Copyright (c) 2014, HerongYang.com, All Rights Reserved. */ import java.util.*; class UsingGenericClass { public static void main(String[] a) { java.io.PrintStream o = System.out; // calling constructors new java.util.Stack<double[]>(); new java.util.ArrayList<String>(); new java.util.Vector<Number>(200); // declaring reference variables java.util.Stack<double[]> stackForDoubleArray; java.util.ArrayList<String> arrayListForString; java.util.Vector<Number> vectorForNumber; vectorForNumber = new java.util.Vector<Number>(200); // passing arguments 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 // receiving returns 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 o.println("First element: "+(Number) firstTry); } }
Compile and run the example, you will get:
First element: 9.99
The output proves that I used generic classes correctly in the example program. Now if you uncomment those 2 not allowed statements, you should see 2 compilation errors:
UsingGenericClass.java:25: error: no suitable method found for add(String) vectorForNumber.add(new String("9.99")); ^ method Collection.add(Number) is not applicable (argument mismatch; String cannot be converted to Number) method List.add(Number) is not applicable (argument mismatch; String cannot be converted to Number) method AbstractCollection.add(Number) is not applicable (argument mismatch; String cannot be converted to Number) method AbstractList.add(Number) is not applicable (argument mismatch; String cannot be converted to Number) method Vector.add(Number) is not applicable (argument mismatch; String cannot be converted to Number) UsingGenericClass.java:32: error: incompatible types: Number cannot be converted to Double Double secondTry = vectorForNumber.firstElement(); ^ Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output 2 errors
The first error says that the parameterized class represented by vectorForNumber supports "add(Number)" method. But error statement tries to invoke it with "add(String)".
The second error says that the parameterized class represented by vectorForNumber supports "Number firstElement()" method. But error statement tries to cast the returning type "Number" to "Double".
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