How to use Wildcard in Java generics

Description

To create a generic method, you use the wildcard argument. The wildcard argument is specified by the ?, and it represents an unknown type.

Wildcard arguments can be bounded in the same way that a type parameter can be bounded. A bounded wildcard is important when creating a generic type that will operate on a class hierarchy.

A bounded wildcard specifies either an upper bound or a lower bound for the type argument.

Syntax

In general, to establish an upper bound for a wildcard, use the following type of wildcard expression:


<? extends superclass>

superclass is the name of the class that serves as the upper bound. This is an inclusive clause.

You can specify a lower bound for a wildcard by adding a super clause to a wildcard declaration.


<? super subclass>

In this case, only classes that are superclasses of subclass are acceptable arguments. This is an exclusive clause, because it will not match the class specified by subclass.

Example

An upper bound for a wildcard.


class Calculator<T extends Number> {
  T[] nums;//  w  w w .  ja v  a2s  .  c om
  Calculator(T[] o) {
    nums = o;
  }
  double average() {
    double sum = 0.0;
    for (int i = 0; i < nums.length; i++){
      sum += nums[i].doubleValue();
    }
    return sum / nums.length;
  }
}
public class Main {
  boolean sameAvg(Calculator<?> ob) {
    if (1.2 == ob.average())
      return true;
    return false;
  }

  public static void main(String args[]) {
  }
}

Calculator<?> matches any Stats object, allowing any two Stats objects to have their averages compared.

The following program demonstrates this:


class Calculator<T extends Number> {
  T[] nums;//w ww . j av a2 s . co m
  Calculator(T[] o) {
    nums = o;
  }
  double average() {
    double sum = 0.0;
    for (int i = 0; i < nums.length; i++)
      sum += nums[i].doubleValue();
    return sum / nums.length;
  }
  boolean sameAvg(Calculator<?> ob) {
    if (average() == ob.average())
      return true;
    return false;
  }
}
public class Main {
  public static void main(String args[]) {
    Integer inums[] = { 1, 2, 3, 4, 5 };
    Calculator<Integer> iob = new Calculator<Integer>(inums);

    Double dnums[] = { 1.1, 2.2, 3.3, 4.4, 5.5 };
    Calculator<Double> dob = new Calculator<Double>(dnums);

    if (iob.sameAvg(dob))
      System.out.println("are the same.");
    else
      System.out.println("differ.");
  }
}

Example 2


class TwoD {//  ww  w . j a  v a 2  s . c o m
  int x, y;
  TwoD(int a, int b) {
    x = a;
    y = b;
  }
}

// Three-dimensional coordinates.
class ThreeD extends TwoD {
  int z;

  ThreeD(int a, int b, int c) {
    super(a, b);
    z = c;
  }
}
class Map<T extends TwoD> {
  T[] coords;

  Map(T[] o) {
    coords = o;
  }
}

public class Main {
  static void showXY(Map<?> c) {
    for (int i = 0; i < c.coords.length; i++){
      System.out.println(c.coords[i].x + " " + c.coords[i].y);
    }      
  }
  static void showXYZ(Map<? extends ThreeD> c) {
    for (int i = 0; i < c.coords.length; i++){
      System.out.println(c.coords[i].x + " " + c.coords[i].y + " "
          + c.coords[i].z);
    }
  }
  public static void main(String args[]) {
    TwoD td[] = { new TwoD(0, 0), new TwoD(-1, -2) };
    Map<TwoD> map = new Map<TwoD>(td);
    System.out.println("Contents of tdlocs.");

    showXY(map);
  }
}

The code above generates the following result.





















Home »
  Java Tutorial »
    Java Language »




Java Data Type, Operator
Java Statement
Java Class
Java Array
Java Exception Handling
Java Annotations
Java Generics
Java Data Structures