Wildcards in Java

In Java generics, there is an option of using “?” in case of unknown types. This “?” is known as a wildcard in Java programming.

Generic programming refers to the programming in which algorithms( step by step execution of the program) are written in terms of types so that program can work for all the Data-types(int, char, String, boolean, etc) not just for one data-type.

We use Wildcards in case of declaring the type of parameter, field, local variable, return type.

Types of Wildcards in Java

  1. Upper Bounded wildcards
  2. Lower Bounded wildcards
  3. Unbounded wildcards

Upper Bound Wildcard:: Suppose we want to create a method that works on 3 lists,

  • List<integer>, List<double>,and List<Number>

Then we can work on this method using Upper Bounded Wildcard.

For using Upper Bound Wildcard we use <? extends Number> in the declaration of the method as the parameter.

eg.       public static double method_name(List<? extends Number> list){

}

Here the list is the object of List.

Now Suppose we want to create product method for 2 lists,

  • The first list contains the values of a data-type integer.
  • The Second list contains values of the data-type double.

Now if we want to find the product of values contained in the list we will create a product method.

package javaapplication28;
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.List;

public class JavaApplication28 {



    public static void main(String[] args) {
     List<Integer> list1=Arrays.asList(1,2,3,4,5,6);
System.out.println("product of the number of list f integers"+product(list1));

List<Double> list2=Arrays.asList(1.1,2.2,3.3,4.4,5.5);
System.out.println("product of the numbers of list which contains double values"+product(list2)); 



    }

    private static double product(List<? extends Number> list3) {
   double product=1.0;
   for(Number i:list3){
       product*=i.doubleValue();
   }return product;
    }
    
}

Explanation of the code

First of all, we created a package. Then created a class inside the package.

In the main method of the class, we created two lists. The first list contains the integer values and the Second list contains double values(according to the above example).

list1 is the object of the first list and list2 is the object of the second list,

the product is the name of the method and private is access specifier and double is data-type.

List<? extends Number> is used in the case of UpperBounded wildcard.

Then using the for-each loop we found the product of the numbers inside the lists.

We use System.out.println(product(list1)) for displaying the product.  product() is the name of method and list1 is the parameter or we can say the object of list whose product we want to find.

output:

 

2. Lower Bounded Wildcards:: We again use “?” to express the Lower Bounded Wildcard.

Suppose we have 2 lists of datatype integer and Number. In the case of Lower Bounded Wildcard, we can only use integer data-type or its Super class. In the case of a data-type integer, its super class is Number.

If we will use any other data-type rather than integer and Number we will get a compilation error.

Syntax::collection-type<? super A> is used as a parameter of the method.

Suppose we want to print elements of two lists, the First list contains integer values, and the Second list contains values of data-type Number.

 

Code:

package javaapplication28;

import java.util.Arrays;
import java.util.List;

public class NewClass {
    public static void main(String args[]){
    List<Integer> list1=Arrays.asList(1,2,3,4);
    printOnly(list1);
    List<Number> list2=Arrays.asList(9,8,7,6);
    printOnly(list2);
    
        
    }
     public static void printOnly(List<? super Integer> list3){
        System.out.println(list3);
}
}

Explanation of the above code

First of all, we created a package and imported all the required packages. Then created the class of any name.

In the main method of the class, we declared two lists. list1 is the object of the first list and list2 is the object of the second list.

Then created the method printOnly() with parameter List<? super Integer> which we require in case of Lower Bounded Wildcards.

We use System.out.println() in the method printOnly() with parameter list3 for printing the values of lists by calling the method printOnly(list1) and printOnly(list2).

Output:

 

 3. Unbounded Wildcard:: We again use  “?” for specifying unbounded wildcard. This means a list of unknown data-types.

Syntax:: List<?>

We use the unbounded wildcard in the case when we want to use the method which does not depend on any type parameter.

And the case in which we want to create a method in which we want to use components of the object class.

 

Code::

package javaapplication28;

import java.util.Arrays;
import java.util.List;

public class NewClass {
    public static void main(String args[]){
    List<Double> list1=Arrays.asList(1.1,2.2,3.3,4.4);
    printOnly1(list1);
    List<Number> list2=Arrays.asList(9,8,7,6);
    printOnly1(list2);
    
        
    }
     public static void printOnly1(List<?> list3){
        System.out.println(list3);
}
}

In the above code, we imported the required packages and then created the class of any name.

In the main method of the class, we created two lists, list1 is the object of the First list and list2 is the object of the second list.

printOnly1() is the method which we use for printing the values of lists.

We use List<?> in the case of Unbounded Wildcard as a parameter of our method.

To print values of lists we call method printOnly1(list1) and printOnly(list2) with the object of Lists as the parameter.

 

Output:

 

Leave a Reply

Your email address will not be published. Required fields are marked *