Java Wildcard – Types of Wildcard in Java
Get Job-ready: Java Course with 45+ Real-time Projects! - Learn Java
One of the most important and useful concepts of Java Generics is the Java wildcard. There are three types of wildcards in Java: Upper bounded wildcards, Lower Bounded Wildcards, and Unbounded Wildcards. In this article, we will vividly discuss all these types of wildcards in Java.
Wildcard in Java:
Basically in Java question marks are known as Wildcards which we can use in generic programming. Wildcards are used to represent the unknown type. We can use java wildcards in a type of parameter, local variable, or field and also as a return type.
The java generic types are not compatible with one another, this is one of the differences between them and arrays. This incompatibility is removed by using the wildcard(?) as an actual parameter.
There are two types of parameters that a programmer can pass to a method in the form of in and out parameters, they are as follows:
1. in variable:
in variable is a variable that provides the program with the actual data to work with. Let us consider a method, path(source, destination), in this method the source variable acts as the in variable, as it provides the data to work with.
2. out variable:
The out variable is a variable that calculates and updates the data provided by the in variable. Let us consider the same method, path(source, destination), in this method the destination variable acts as the out variable, as it calculates the destination path with the help of the data provided by the source variable, and updates it.
Types of Wildcard in Java
1. Upper Bounded Wildcards
2. Lower Bounded Wildcards
3. Unbounded Wildcards
Let us discuss them one by one.
Upper Bounded Wildcards:
The upper Bounded Wildcards helps us relax the restriction on the type of variable in the method. If we want to relax the restriction on the variable type, we use this type of wildcards.
Let us take an example where we take a method sum in which we use the upper bound wildcard to take any type of variable as input. We can do this by using the ? character followed by the extends keyword.
Syntax:
private static double sum(List < ?extends Number > my_List)
Code to understand the above example:
package com.DataFlair.Wildcards; import java.util.*; public class Upperbound { public static void main(String[] args) { List < Integer > integer_List = Arrays.asList(1, 3, 5, 7);//Integer List System.out.println("Total sum is:" + sum(integer_List)); List < Double > double_List = Arrays.asList(1.2, 3.4, 5.6, 7.8);//Double List System.out.print("Total sum is: " + sum(double_List)); } private static double sum(List < ?extends Number > my_List)//Using ? to relax restriction of input type. { double sum = 0.0; for (Number iterator: my_List) { sum = sum + iterator.doubleValue(); } return sum; } }
The output of the above code:
Total sum is: 18.0
Lower Bounded Wildcards:
To widen the use of the type of variable, we can use the Lower Bounded Wildcards.
For example: If we use List<Integer>, we will be able to take only a list of integers and not any other type of variables. Using Lower Bounded Wildcards we can use List<Number> and List<Object> to take integer input. This also allows us to widen the type of variable and let us take any type of variable. This can be achieved by using the ? character followed by the super keyword.
Syntax:
public static void printLowerBounded(List < ?super Integer > LowerBounded_list)
Code to understand the above example:
package com.DataFlair.Wildcards; import java.util.*; public class Lowerbound { public static void main(String[] args) { List < Integer > int_List = Arrays.asList(3, 5, 7, 9); printLowerBounded(int_List); List < Number > number_List = Arrays.asList(10.5, 20.2, 30.3, 40.5); printLowerBounded(number_List); } public static void printLowerBounded(List < ?super Integer > LowerBounded_list) { System.out.println(LowerBounded_list); } }
The output of the above code:
[10.5, 20.2, 30.3, 40.5]
Unbounded Wildcards:
We can use the unbounded Wildcard to specify the type of wildcard along with the wildcard character(?). We generally use this wildcard when the code inside the method is using the objects and when the execution of the method does not depend upon the parameter type.
Syntax:
private static void print_List(List < ?>Unbounded_list)
Code to understand Unbounded Wildcards:
package com.DataFlair.Wildcards; import java.util.*; public class Unbounded { public static void main(String[] args) { List < Integer > int_List = Arrays.asList(1, 3, 5, 7);//Integer List List < Double > double_List = Arrays.asList(10.2, 15.3, 20.4, 25.5);//Double list print_List(int_List); print_List(double_List); } private static void print_List(List < ?>Unbounded_list)//unbounded wildcard used { System.out.println(Unbounded_list); } }
The output of the above code:
[10.2, 15.3, 20.4, 25.5]
Let us summarize the Syntax of the three WildCards:
1. Upper Bound Wildcard:
<? extends Type>
2. Lower bound Wildcard:
<? super <Type>
3. Unbounded Wildcard:
<?>
Guidelines for Using Wildcard in Java:
It is important to understand when to use which kind of Wildcard:
- If there is an in variable in the parameter, we use the extends keyword with the wildcard character, i.e, Upper bound Wildcard.
- When there is an out variable in the parameter, we use the super keyword with the wildcard character, i.e, Lower bound Wildcard.
- If the variables can be accessed with the Object class method, then we use only the wildcard character, i.e., Unbounded wildcard.
- If there are both in and out variables in the parameter, then we do not need to use any wildcard.
Conclusion:
In this article, we saw what exactly are wildcards, and also the different types of wildcards. We also discussed when to use which wildcard. The wildcard is used along with Java generics. It is a very important concept to handle parameters according to our needs.
Your opinion matters
Please write your valuable feedback about DataFlair on Google
Thanks a lot for tutorial.
Hi Rehman,
Thank you for Visiting Data-Flair and Appreciate our “Java Wildcard” Tutorial. We hope you are not stopped here, keep exploring Data-Flair Catagories, there is much more information and blogs on Java Programming Langauge.
Regard,
Data-Flair