filmov
tv
Learn Java Tutorial for Beginners, Part 35: Generics and Wildcards

Показать описание
In this video by Quordnet Academy for the series Learn Java Tutorial for Beginners how to use Using Generics and Wildcards have been discussed.
If you would like to discover even more about java programming tutorial or the java complete course I advise you to check out the playlist :
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
class WildcardDemo
{
public static void main(String[] args)
{
//Upper Bounded Integer List
//printing the sum of elements in list
//Double list
//printing the sum of elements in list
}
private static double sum(List(? extends Number) list)
{
double sum=0.0;
for (Number i: list)
{
}
return sum;
}
}
Output:
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)
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.
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
Possibly if you have doubt comment below and let me understand what else I can help you with information in java.This playlist is a full fledged java programming for beginners and java tutorial for beginners which is given above.
Please share with your friends the video to assist other people looking for java programming tutorial or object oriented programming java .
To never miss an update from or channel hit the subscribe button first and if already subscribe hit the bell icon.
1.Follow us on INSTAGRAM for Interesting posts
2.Follow us on LINKEDIN for interesting content on different aspects
3.Don't forget to like our FACEBOOK to get the most out of it
4.Follow us on twitter to get a mix of all
5.If you want to get us on TUMBLR please then click on the link given below
6.Do join our OFFICIAL Telegram for notes of different things
7.For get job update regularly both private and government do join this telegram channel
#quordnetacademy, #java_tutorial_series, #javatutorialseries
If you would like to discover even more about java programming tutorial or the java complete course I advise you to check out the playlist :
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
class WildcardDemo
{
public static void main(String[] args)
{
//Upper Bounded Integer List
//printing the sum of elements in list
//Double list
//printing the sum of elements in list
}
private static double sum(List(? extends Number) list)
{
double sum=0.0;
for (Number i: list)
{
}
return sum;
}
}
Output:
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)
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.
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
Possibly if you have doubt comment below and let me understand what else I can help you with information in java.This playlist is a full fledged java programming for beginners and java tutorial for beginners which is given above.
Please share with your friends the video to assist other people looking for java programming tutorial or object oriented programming java .
To never miss an update from or channel hit the subscribe button first and if already subscribe hit the bell icon.
1.Follow us on INSTAGRAM for Interesting posts
2.Follow us on LINKEDIN for interesting content on different aspects
3.Don't forget to like our FACEBOOK to get the most out of it
4.Follow us on twitter to get a mix of all
5.If you want to get us on TUMBLR please then click on the link given below
6.Do join our OFFICIAL Telegram for notes of different things
7.For get job update regularly both private and government do join this telegram channel
#quordnetacademy, #java_tutorial_series, #javatutorialseries