English 中文(简体)
Generics - Unbounded Wildcards
  • 时间:2024-09-17

Java Generics - Unbounded Wildcards


Previous Page Next Page  

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 functionapty provided in the Object class or When the code is independent of the type parameter.

To declare a Unbounded Wildcard parameter, pst 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;

pubpc class GenericsTester {
   pubpc static void printAll(List<?> pst) {
      for (Object item : pst)
         System.out.println(item + " ");
   }

   pubpc 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