Java Tutorials - Herong's Tutorial Examples - Version 7.03, by Dr. Herong Yang
Wildcard Parameterized Type Test
This section provides a tutorial example on how to declare variables using wildcard parameterized types.
The supertype-subtype relationships between wildcard parameterized types and parameterized types are confusing. So let's try a tutorial example:
/* WildcardParameterizedType.java - Copyright (c) 2014, HerongYang.com, All Rights Reserved. */ import java.util.*; class WildcardParameterizedType { public static void main(String[] a) { // remember class Integer extends Number {} // allowed - implicit casting from subtype to supertype Vector<?> unbounded = new Vector<String>(); Vector<? extends Number> upperBounded = new Vector<Integer>(); Vector<? super Integer> lowerBounded = new Vector<Number>(); unbounded = lowerBounded; System.out.println("Initial capacity: "+unbounded.capacity()); // not allowed - compilation error // Vector<String> paramString = unbounded; // Vector<Integer> paramInteger = upperBounded; // Vector<Number> paramNumber = lowerBounded; } }
If you compile and run this example as is, you will not see issues.
But if you uncomment those not allowed lines, you will see these compilation errors with JDK 1.8:
WildcardParameterizedType.java:18: error: incompatible types: Vector<CAP#1> cannot be converted to Vector<String> Vector<String> paramString = unbounded; ^ where CAP#1 is a fresh type-variable: CAP#1 extends Object from capture of ? WildcardParameterizedType.java:19: error: incompatible types: Vector<CAP#1> cannot be converted to Vector<Integer> Vector<Integer> paramInteger = upperBounded; ^ where CAP#1 is a fresh type-variable: CAP#1 extends Number from capture of ? extends Number WildcardParameterizedType.java:20: error: incompatible types: Vector<CAP#1> cannot be converted to Vector<Number> Vector<Number> paramNumber = lowerBounded; ^ where CAP#1 is a fresh type-variable: CAP#1 extends Object super: Integer from capture of ? super Integer 3 errors
The compiler refuses to down cast the wildcard parameterized type back to the parameterized type. But you can try to do explicit down casting like Vector<Integer> paramInteger = (Vector<Integer>) upperBounded; The compiler will still give you "unchecked or unsafe operations" warnings.
Note that if you are using JDK 1.7, you will get slightly different error messages:
WildcardParameterizedType.java:18: error: incompatible types Vector<String> paramString = unbounded; ^ required: Vector<String> found: Vector<CAP#1> where CAP#1 is a fresh type-variable: CAP#1 extends Object from capture of ? WildcardParameterizedType.java:19: error: incompatible types Vector<Integer> paramInteger = upperBounded; ^ required: Vector<Integer> found: Vector<CAP#1> where CAP#1 is a fresh type-variable: CAP#1 extends Number from capture of ? extends Number WildcardParameterizedType.java:20: error: incompatible types Vector<Number> paramNumber = lowerBounded; ^ required: Vector<Number> found: Vector<CAP#1> where CAP#1 is a fresh type-variable: CAP#1 extends Object super: Integer from capture of ? super Integer 3 errors
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