
- Generics - Home
- Generics - Overview
- Generics - Environment Setup
- Examples - Generic Classes
- Generics - Generic Classes
- Type Parameter Naming Conventions
- Generics - Type inference
- Generics - Generic Methods
- Generics - Multiple Type Parameters
- Generics - Parameterized Types
- Generics - Raw Types
- Examples - Bounded Type Parameters
- Bounded Type Parameters
- Generics - Multiple Bounds
- Examples - Collections
- Generics - Generic List
- Generics - Generic Set
- Generics - Generic Map
- Examples - Wild Cards
- Upper Bounded Wildcards
- Generics - Unbounded Wildcards
- Lower Bounded Wildcards
- Generics - Guidelines for Wildcards
- Type Erasure
- Generics - Generic Types Erasure
- Generics - Bound Types Erasure
- Unbounded Types Erasure
- Generics - Generic Methods Erasure
- Restrictions on Generics
- Generics - No Primitive Types
- Generics - No Instance
- Generics - No Static field
- Generics - No Cast
- Generics - No instanceOf
- Generics - No Array
- Generics - No Exception
- Generics - No Overload
- Generics Useful Resources
- Generics - Quick Guide
- Generics - Useful Resources
- Generics - Discussion
Java Generics - Unbounded Wildcards
The question mark (?), represents the wildcard, stands for unknown type in generics. There may be times when any object can be used when a method can be implemented using functionality provided in the Object class or When the code is independent of the type parameter.
To declare a Unbounded Wildcard parameter, list the ? only.
Example
Following example illustrates how extends is used to specify an unbounded wildcard.
package com.tutorialspoint; import java.util.Arrays; import java.util.List; public class GenericsTester { public static void printAll(List<?> list){ for (Object item : list) System.out.println(item + " "); } public static void main(String args[]) { List<Integer> integerList = Arrays.asList(1, 2, 3); printAll(integerList); List<Double> doubleList = Arrays.asList(1.2, 2.3, 3.5); printAll(doubleList); } }
This will produce the following result −
Output
1 2 3 1.2 2.3 3.5
Advertisements