Java Generics Tutorial
Selected Reading
- Java Generics - Discussion
- Java Generics - Useful Resources
- Java Generics - Quick Guide
- Java Generics - No Overload
- Java Generics - No Exception
- Java Generics - No Array
- Java Generics - No instanceOf
- Java Generics - No Cast
- Java Generics - No Static field
- Java Generics - No Instance
- Java Generics - No Primitive Types
- Java Generics - Methods Erasure
- Unbounded Types Erasure
- Java Generics - Bound Types Erasure
- Java Generics - Types Erasure
- Generics - Guidelines for Wildcards
- Lower Bounded Wildcards
- Generics - Unbounded Wildcards
- Upper Bounded Wildcards
- Java Generics - Generic Map
- Java Generics - Generic Set
- Java Generics - Generic List
- Java Generics - Multiple Bounds
- Bounded Type Parameters
- Java Generics - Raw Types
- Java Generics - Parameterized Types
- Java Generics - Multiple Type
- Java Generics - Generic Methods
- Java Generics - Type inference
- Type Parameter Naming Conventions
- Java Generics - Generic Classes
- Java Generics - Environment Setup
- Java Generics - Overview
- Java Generics - Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Upper Bounded Wildcards
Java Generics - Upper Bounded Wildcards
The question mark (?), represents the wildcard, stands for unknown type in generics. There may be times when you ll want to restrict the kinds of types that are allowed to be passed to a type parameter. For example, a method that operates on numbers might only want to accept instances of Number or its subclasses.
To declare a upper bounded Wildcard parameter, pst the ?, followed by the extends keyword, followed by its upper bound.
Example
Following example illustrates how extends is used to specify an upper bound wildcard.
package com.tutorialspoint; import java.util.Arrays; import java.util.List; pubpc class GenericsTester { pubpc static double sum(List<? extends Number> numberpst) { double sum = 0.0; for (Number n : numberpst) sum += n.doubleValue(); return sum; } pubpc static void main(String args[]) { List<Integer> integerList = Arrays.asList(1, 2, 3); System.out.println("sum = " + sum(integerList)); List<Double> doubleList = Arrays.asList(1.2, 2.3, 3.5); System.out.println("sum = " + sum(doubleList)); } }
This will produce the following result −
Output
sum = 6.0 sum = 7.0Advertisements