Wildcards in Java

Course Curriculum

Wildcards in Java

Wildcards in Java

The question mark (?) is known as the wildcard in generic programming . It represents an unknown type. The wildcard can be used in a variety of situations such as the type of a parameter, field, or local variable; sometimes as a return type. Unlike arrays, different instantiations of a generic type are not compatible with each other, not even explicitly. This incompatibility may be softened by the wildcard if ? is used as an actual type parameter.

Types of wildcards in Java:

  • Upper Bounded Wildcards: These wildcards can be used when you want to relax the restrictions on a variable. For example, say you want to write a method that works on List < integer >, List < double >, and List < number > , you can do this using an upper bounded wildcard.
  • To declare an upper-bounded wildcard, use the wildcard character (‘?’), followed by the extends keyword, followed by its upper bound.
    public static void add(List<? extends Number> list)
    Implementation:

//Java program to demonstrate Upper Bounded Wildcards
import java.util.Arrays;
import java.util.List;

class WildcardTest
{
public static void main(String[] args)
{

//Upper Bounded Integer List
List<Integer> list1= Arrays.asList(9, 8, 5, 2);

//printing the sum of elements in list
System.out.println("Total sum is:"+sum(list1));

//Double list
List<Double> list2=Arrays.asList(4.1,5.1,6.1);

//printing the sum of elements in list
System.out.print("Total sum is:"+sum(list2));
}

private static double sum(List<? extends Number> list)
{
double sum=0.0;
for (Number i: list)
{
sum+=i.doubleValue();
}

return sum;
}
}
Result :

Total sum is:22.0
Total sum is:15.299999999999999

  • In the above program, list1 and list2 are objects of the List class. list1 is a collection of Integer and list2 is a collection of Double. Both of them are being passed to method sum which has a wildcard that extends Number. This means that list being passed can be of any field or subclass of that field. Here, Integer and Double are subclasses of class Number.
  • Lower Bounded Wildcards: It is expressed using the wildcard character (‘?’), followed by the super keyword, followed by its lower bound: <? super A>.
    Syntax: Collectiontype <? super A>
    Implementation:

//Java program to demonstrate Lower Bounded Wildcards
import java.util.Arrays;
import java.util.List;

class WildcardDemo
{
public static void main(String[] args)
{
//Lower Bounded Integer List
List<Integer> list1= Arrays.asList(9, 8, 5, 2);

//Integer list object is being passed
printOnlyIntegerClassorSuperClass(list1);

//Number list
List<Number> list2= Arrays.asList(9, 8, 5, 2);

//Integer list object is being passed
printOnlyIntegerClassorSuperClass(list2);
}

public static void printOnlyIntegerClassorSuperClass(List<? super Integer> list)
{
System.out.println(list);
}
}
Result :

[9, 8, 5, 2]
[9, 8, 5, 2]

  • Here arguments can be Integer or superclass of Integer(which is Number). The method printOnlyIntegerClassorSuperClass will only take Integer or its superclass objects. However if we pass list of type Double then we will get compilation error. It is because only the Integer field or its superclass can be passed .
  • Double is not the superclass of Integer.
  • Use extend wildcard when you want to get values out of a structure and super wildcard when you put values in a structure. Don’t use wildcard when you get and put values in a structure.
  • Note: You can specify an upper bound for a wildcard, or you can specify a lower bound, but you cannot specify both.

Unbounded Wildcard: This wildcard type is specified using the wildcard character (?), for example, List. This is called a list of unknown type. These are useful in the following cases

  • When writing a method which can be employed using functionality provided in Object class.
  • When the code is using methods in the generic class that don’t depend on the type parameter

Implementation:

//Java program to demonstrate Unbounded wildcard
import java.util.Arrays;
import java.util.List;

class unboundedwildcardemo
{
public static void main(String[] args)
{

//Integer List
List<Integer> list1= Arrays.asList(1,2,3);

//Double list
List<Double> list2=Arrays.asList(1.1,2.2,3.3);

printlist(list1);

printlist(list2);
}

private static void printlist(List<?> list)
{

System.out.println(list);
}
}
Result :

[1, 2, 3]
[1.1, 2.2, 3.3]

(Next Lesson) How to start learning Java