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 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:

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 input variable, as it provides the data to work with.

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 Wildcards 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 help 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 wildcard.

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. Can we do this by using the? character followed by the extends keyword.

Syntax of upper-bounded wildcards in Java:

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:16.0
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 of lower-bounded wildcards in Java:

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:

[3, 5, 7, 9]
[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 of unbounded wildcards in Java:

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:

[1, 3, 5, 7]
[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.
  • Wildcards are not meant to be used as a return value. If it is used as a return type, it will make the processing of the API harder because wildcards are not of any type.
  • Only a single bound can be used in a wildcard. Attempting to use more than one bound will raise an error.
  • Two operations cannot be performed on a wildcard.

Conclusion

In this article, we saw what exactly wildcards are 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.

Did you like this article? If Yes, please give DataFlair 5 Stars on Google

courses

DataFlair Team

DataFlair Team creates expert-level guides on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our goal is to empower learners with easy-to-understand content. Explore our resources for career growth and practical learning.

2 Responses

  1. Rahman says:

    Thanks a lot for tutorial.

    • Data Flair says:

      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

Leave a Reply

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