Wildcards in Java with Examples

When it comes to Java Generics, the wildcard plays a vital role. To understand the working of wildcards in java and how to implement it in real-time programs, this article will help you. .Let’s start!!!

What is Wildcard in Java?

The question marks to denote the unknown type in generic programming are known as Wildcards. These Wildcards take place instead of parameters, local variables, fields, and even as return types.

In contrast to arrays, the diversified instantiations of a generic type are not compatible with one another.

We cannot use wildcards when the generic class is instantiated or the generic method is called. Within the same generic type, it plays a vital role in removing the incompatibility between instantiations. The wilds cards accomplish this task as an actual parameter.

Types of parameters in java wildcards:

There are two types of parameters that we usually pass to a method as in and out parameters. They are:

1. in variable:

The in variable offers the actual data to the Java program. For example, consider the method method1(source, destination). Here, the source acts as an invariable to method1.

2. out variable:

The out variable stores the updated data done by the Java program. For example, in the method method1(source, destination), destination acts as an out variable.

Types of Wildcard in Java:

There are three different categories of wildcards:

1. Upper Bounded Wildcards

2. Lower Bounded Wildcards

3. Unbounded Wildcards

1. Upper Bounded Wildcards in java

This category of Wildcards relaxes the restriction of the variable’s type. Therefore, to relax the restriction on the type of the variable in the method, we can make use of this type of wildcards.

Syntax:

public static void add(List<? Extends Number list)

Sample program to implement upper bound wildcards:

public class FirstCode{
public static void main(String args[]){
List<Integer> l = Arrays.asList(5, 10, 15, 20);
System.out.println(“The sum is: ”+sum(l));
List<Double> l2 = Arrays.asList(2.5, 12.3, 16.7, 5.8);
System.out.println(“The sum is: ”+sum(l2));
}
private static double sum(List <?extends Number> firstList){
double sum = 0.0;
for(Number iterator: firstList){
sum =  sum+iterator.l2();
}
return sum;
}
}

Output:

The sum is: 50
The sum is: 37.3

2. Lower Bounded Wildcards in java

This category of wildcards helps in widening the use of the type of variable. For example, to add only a list of integers in our method, we can use List<Integer>. This makes the method bound to use only the list of integers. It does not involve any other data type.

Syntax:

Collectiontype(<? super A)

Sample program to implement lower bounded wildcards:

import java.io.*;
public class FirstCode{
public static void main(String args[]){
List<Integer> l = Arrays.asList(5, 10, 15, 20);
printOnlyIntegerClassorSuperClass(l);
List<Number> l = Arrays.aslist(5, 10, 15, 20);
printOnlyIntegerClassorSuperClass(l);
}
public static void printOnlyIntegerClassorSuperClass(List <? Super Integer> list){
System.out.println(list);
}
}

Output:

[5, 10, 15, 20]
[5, 10, 15, 20]

3. Unbounded Wildcards in java

The unbounded wildcards are used when we need to mention the type of the wildcard along with the wildcard character ‘?’. We usually use this when the code given in the method uses Object functionality. It is also used when the code within the method is independent of the parameter type.

Sample program to implement unbounded wildcards:

import java.util.*;
public class FirstCode{
public static void main(String args[]){
List<Integer> l = Arrays.asList(5, 10, 15, 20);
List<Double> l2 = Arrays.asList(5.5, 10.5, 15.5,16.5);
printList(l);
printList(l2);
}
private static void printList(List <?>list){
System.out.println(list);
}
}

Output:

[5, 10, 15, 20]
[5.5, 10.5, 15.5, 20.5]

Uses of Wildcard in Java:

1. Upper Bound Wildcard: If the variable comes under in type, the usage of the ‘extends’ keyword with a wildcard takes place here.

2. Lower bound Wildcard: If the variable comes under out type, it is an out variable. Here, we use the ‘super’ keyword with a wildcard.

3. Unbounded wildcard: If we access a variable using the Object Class method, it is better to prefer the unbounded wildcards.

4. No wildcard: When the wildcard belongs to both in and out categories, we need not use any wildcards.

Conclusion:

These are the points you need to know about wildcards in java. You can choose the best-suited type of wildcard according to the type of the variable. I hope this article has explained the wildcards in Java in a clear way.

Leave a Reply

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