What are Unbounded wildcard w.r.t Generics method in Java?


Generics is a concept in Java where you can enable a class, interface and, method, accept all (reference) types as parameters. In other words it is the concept which enables the users to choose the reference type that a method, constructor of a class accepts, dynamically. By defining a class as generic you are making it type-safe i.e. it can act up on any datatype.

To define a generic class you need to specify the type parameter you are using in the angle brackets “<>” after the class name and you can treat this as datatype of the instance variable an proceed with the code.

Example

 Live Demo

class Student<T>{
   T age;
   Student(T age){
      this.age = age;
   }
   public void display() {
      System.out.println("Value: "+this.age);
   }
}
public class GenericsExample {
   public static void main(String args[]) {
      Student<Float> std1 = new Student<Float>(25.5f);
      std1.display();
      Student<String> std2 = new Student<String>("25");
      std2.display();
      Student<Integer> std3 = new Student<Integer>(25);
      std3.display();
   }
}

Output

Value: 25.5
Value: 25
Value: 25

Wildcards

Instead of the typed parameter in generics (T) you can also use “?”, representing an unknown type. You can use a wild card as a −

  • Type of parameter.
  • Field
  • Local field.

The only restriction on wilds cards is that you cannot it as a type argument of a generic method while invoking it.

Java provides 3 types of wild cards namely upper-bounded, lower-bounded, un-bounded.

Unbounded wildcards

An unbounded wildcard is the one which enables the usage of all the subtypes of an unknown type i.e. any type (Object) is accepted as typed-parameter.

For example, if want to accept an ArrayList of object type as a parameter, you just need to declare an unbounded wildcard.

To create/declare a Unbounded wildcard, you just need to specify the wild card character “?” as a typed parameter within angle brackets.

Example

Following Java example demonstrates the creation of the Unbounded wildcard.

import java.util.List;
import java.util.Arrays;
public class UnboundedExample {
   public static void sampleMethod(List<?> col){
      for (Object ele : col) {
         System.out.print(ele+" ");
      }
      System.out.println("");
   }
   public static void main(String args[]) {
      ArrayList<Integer> col1 = new ArrayList<Integer>();
      col1.add(24);
      col1.add(56);
      col1.add(89);
      col1.add(75);
      col1.add(36);
      sampleMethod(col1);
      ArrayList<Double> col2 = new ArrayList<Double>();
      col2.add(24.12d);
      col2.add(56.25d);
      col2.add(89.36d);
      col2.add(75.98d);
      col2.add(36.47d);
      sampleMethod(col2);
   }
}

Output

24 56 89 75 36
24.12 56.25 89.36 75.98 36.47

If you pass an List object created from arrays (contains elements of primitive type) a compile time error will be generated.

Example

 Live Demo

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class UnboundedExample {
   public static void sampleMethod(List<?> col){
      for (Object ele : col) {
         System.out.print(ele+" ");
      }
      System.out.println("");
   }
   public static void main(String args[]) {
      ArrayList<Integer> col1 = new ArrayList<Integer>();
      col1.add(24);
      col1.add(56);
      col1.add(89);
      col1.add(75);
      col1.add(36);
      sampleMethod(col1);
      ArrayList<Double> col2 = new ArrayList<Double>();
      col2.add(24.12d);
      col2.add(56.25d);
      col2.add(89.36d);
      col2.add(75.98d);
      col2.add(36.47d);
      sampleMethod(col2);
      List<Object> col2 = Arrays.asList(22.1f, 3.32f, 51.4f, 82.7f, 95.4f, 625.f);
      sampleMethod(col2);
   }
}

Compile time error

UnboundedExample.java:27: error: variable col2 is already defined in method main(String[])
      List<Object> col2 = Arrays.asList(22.1f, 3.32f, 51.4f, 82.7f, 95.4f, 625.f);
                  ^
1 error
raja
Published on 12-Sep-2019 08:17:35
Advertisements