Search the courses, subjects, tracks, games and features, or jump straight to a page.
Learn it your way
A short class with Sarah and her students. The other ways are under More.
Sign up free to listen. Transcripts are free to read.
Today, we're going to learn about wildcards in generics. What do you think happens when we don't restrict the type, like using a <?> wildcard?
Does it mean we can use any type without restrictions?
Exactly! An unbounded wildcard allows you to accept any type. For instance, if we have a method that prints lists, it can handle List<?>. Why do you think this flexibility is useful?
I guess it makes our code reusable for different types of data?
Right! Reusability is key. Let’s consider a method you might use to print elements of a list. Can you think of how we could define such a method?
"Like this?
Now, let’s move on to upper bounded wildcards. What do you think is the significance of using <? extends Type>?
It restricts the type to a specific class or its subclasses, right?
Exactly! This is useful, for example, when you need to sum numbers without worrying about the specific number subclasses. Can someone describe how you'd write a method that uses this feature?
"Something like:
Finally, let’s discuss lower bounded wildcards. What do you think using <? super Type> allows us to do?
It restricts the type to the specified class or its superclasses?
Correct! This is especially useful for inserting elements into a collection. Can you think of an example where this would be beneficial?
"If I wanted to add integers to a list, I'd use something like:
Using an unbounded wildcard to print a list: public void printList(List<?> list) { ... }.
Summing numbers with an upper bounded wildcard: public double sum(List<? extends Number> list) { ... }.
Adding integers to any superclass list with a lower bounded wildcard: public void addIntegers(List<? super Integer> list) { ... }.
Wildcards are wild and free, for any type they’ll let you be.
Imagine a zoo with various animals. Unbounded wildcards are like a keeper curating all species, while upper bounded wildcards are zookeepers overseeing just cats, and lower bounded wildcards are caretakers managing all types of mammals.
Use the acronym UUL: Unbounded - anything goes, Upper Bounded - subclasses flow, Lower Bounded - super class grow!
Remember the acronym 'WUL' - Wildcards (for various) Usability in Lists.
Term
What does an unbounded wildcard represent?
Click to flip
Term
When would you use an upper bounded wildcard?
Click to flip
Term
What is a lower bounded wildcard used for?
Click to flip
Wildcards in generics provide a flexible way to handle different types while maintaining type safety in Java.
This section describes three types of wildcards in Java generics: unbounded, upper bounded, and lower bounded. Each type allows for specific behaviors and constraints when working with generic code, making programs more adaptable while preserving compile-time type safety.
Wildcards in generics add flexibility to your code in Java by allowing you to create methods and classes that can work with various types without specifying them explicitly. There are three main types of wildcards:
Unbounded Wildcard (<?>): This wildcard can accept any type. For example, a method that processes a list of unknown objects can be declared using List<?>. This is useful when you do not need to know about the specific type of the object being used.
public void printList(List<?> list) {
for (Object obj : list) {
System.out.println(obj);
}
}Upper Bounded Wildcard (<? extends Type>): This wildcard means that you can accept a specific type and its subclasses. It's beneficial when working with a network of class hierarchies. For instance:
public double sum(List<? extends Number> list) {
double total = 0;
for (Number n : list) {
total += n.doubleValue();
}
return total;
}Lower Bounded Wildcard (<? super Type>): This wildcard allows you to accept a specified type and its superclasses. This is commonly used for writing to a collection.
public void addIntegers(List<? super Integer> list) {
list.add(1);
list.add(2);
}Each type of wildcard has its uses, and understanding these is crucial for writing flexible and type-safe generic code.