Java Wildcard – Types of Wildcard in Java

1. Objective

In our last tutorial, we discussed the Java Date and Time. Here, we explore the knowledge of Java Wildcard tutorial, in which we are going to learn what is Java wildcard and how to use a wildcard in Java Programming Language. Moreover, we will discuss types of a wildcard in Java: Upper bounded wildcards, Lower Bounded wildcards, and Unbounded Wildcards in Java with some example of Java wildcard.

So, let’s start the Java Wildcard Tutorial.

Java Wildcard - Types of Wildcard in Java

Java Wildcard – Types of Wildcard in Java

2. What is Java Wildcard?

Wildcards in Java are basically the question mark used in generic programming, it basically represents the unknown type. Java Wildcard used widely in situations such as in type of parameter, local variable, or field and also as a return type.

Different installations of the generic type are not compatible with each other which is unlike in arrays, it can’t be even achieved explicitly, this softened by using a wildcard where it is used as an actual type parameter.

Do you know the difference between JDK vs JRE vs JVM

3. Types of Wildcard in Java

Here, we are going to study types of Java Wildcards, let’s discuss them one by one:

a. Upper Bounded Wildcards

This Java wildcard cases can be utilized when you need to relax the confinements on a variable. For instance, say you need to compose a technique that takes a shot at List < whole number >, List < twofold >, and List < number >, you can do this utilizing an upper limited Java wildcard case.

A Syntax of Upper Bounded Wildcard in Java –

public static void add(List<? extends Number> list)

Read About Serialization and Deserialization in Java Programming Language

Upper Bounded Java Wildcard Example –

import java.util.Arrays;
import java.util.List;
class WildcardDemo
{
    public static void main(String[] args)
    {
        List<Integer> list1= Arrays.asList(4,5,6,7);
        System.out.println("Total sum is:"+sum(list1));
        List<Double> list2=Arrays.asList(4.1,5.1,6.1);
        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;
    }
}

Output –
The total sum is:22.0
The total sum is:15.299999999999999

b. Lower Bounded Wildcards

It is a wildcard (?) followed by a super keyword and then by its lower bound.
A Syntax of Lower Bounded Wildcard in Java–

Collectiontype <? super A>

Let’s discuss Types of Java Exceptions in detail 

Lower Bounded Java Wildcard Example –

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(4,5,6,7);
        printOnlyIntegerClassorSuperClass(list1);
        List<Number> list2= Arrays.asList(4,5,6,7);
        printOnlyIntegerClassorSuperClass(list2);
    }
    public static void printOnlyIntegerClassorSuperClass(List<? super Integer> list)
    {
        System.out.println(list);
    }
}

Output –
[4, 5, 6, 7] [4, 5, 6, 7] Here arguments can be Integer or superclass of Integer(which is Number). The method printOnlyIntegerClassorSuperClass will just take Integer or its superclass objects. In any case in the event that we pass list of sort Double then we will get a compilation error. It is on account of just the Integer field or its superclass can be passed. Double isn’t the superclass of Integer.

Do You Know How Java Inheritance is used?

Utilize extend wildcard when you need to get esteems out of a structure and super trump card when you place esteems in a structure. Try not to utilize wildcard when you get and place values in a structure.

c. Unbounded Wildcard in Java

It is specified using Java wildcard character (?). This is called the unknown type list.  Main uses are –

  • While writing a method whose functionality can be used or provided in an object class.
  • While using a generic code that doesn’t depend on the type parameter.

Unbounded Wildcard in Java example-

import java.util.Arrays;
import java.util.List;
class unboundedwildcardemo
{
    public static void main(String[] args)
    {
        List<Integer> list1= Arrays.asList(1,2,3);
        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);
    }
}

Output –
[1, 2, 3] [1.1, 2.2, 3.3] This was all about Java Wildcard Tutorial. Hope you like our explanation.

Let’s Know about Java Regex – Java Regular Expression with Examples

4. Conclusion

Hence, in this Java wildcard tutorial, we studied what is Wildcard in java. In addition, we also studied Types of java Wildcards: Upper bounded wildcard, Lower Bounded wildcard, Unbounded Wildcard in Java with examples and syntax. Furthermore, if you have any query, feel free to ask in the comment section.

Related Topic- Java Number 

For reference

No 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 *