Java Generic Parameter Wildcard

In this chapter you will learn:

  1. How to use generic wildcard arguments
  2. Syntax for Java Generic Parameter Wildcard
  3. Example - an upper bound for a wildcard
  4. How to use generic bounded wildcards

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;//from  w  w w .  jav  a 2  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;
  }
}
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;/*from  ww  w .  j av a 2s .  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 {//from  w w  w .  j  a v a2  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.

Next chapter...

What you will learn in the next chapter:

  1. How to declare generic interface and implement generic interface
  2. Syntax for Java generic interface
  3. Note for Java Generic interface
  4. Example - Java Generic interface
Home »
  Java Tutorial »
    Java Langauge »
      Java Generics
Java Generic Type
Java Generic Bounded Types
Java Generic Method
Java Generic Constructors
Java Generic Parameter Wildcard
Java Generic interface
Java Raw Types and Legacy Code
Java Generic Class Hierarchies
Java generic types cast
Java Generic Restrictions