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.
Let's start with unbounded wildcards, denoted by <?>. Can anyone tell me when we would use an unbounded wildcard?
Is it for when we're not really sure what type we're going to deal with?
Exactly! It provides flexibility when you don't need to specify a type. It allows any type of object. For example, a method that accepts a List<?> can be called with any type of list.
Could we use it if we need to process a collection regardless of type?
Yes, that's right! For example, you might want to print all elements in a list regardless of their type. You can iterate through a List<?> safely.
Could you give a coding example of that?
Sure! Here’s a simple method that takes in a List<?> and prints out its elements: public void printList(List<?> list) { for (Object obj : list) System.out.println(obj); }.
So to recap, unbounded wildcards allow for flexibility with any type. Great job, everyone!
Next, let's discuss upper bounded wildcards, which use the syntax <? extends T>. Can anyone explain what that means?
It sounds like we're specifying a maximum type. We can only use it for types that are T or its subtypes?
Correct! This is particularly useful when you want to read items of a certain type. For instance, if you have a method that works with List<? extends Number>, what kinds of lists can we pass?
We could pass a List<Integer> or a List<Double>?
Exactly! You can read Number or its subtypes, which is beneficial for operations like computations. Can anyone think of a scenario where this might be helpful?
Maybe in a method that calculates the average of numbers, regardless of the specific number type!
Perfect example! Upper bounded wildcards work when we are producing elements. Just remember: Producer Extends!
Now, let's delve into lower bounded wildcards using <? super T>. What do you think this means, and when would we use it?
It seems like we're allowing only writing types that are T or any supertype of T?
Exactly! This is what we use when we need to add elements. For example, if we have List<? super Integer>, which types can this list accept?
It could accept List<Number> or List<Object> because they are supertypes of Integer!
Exactly! This is great for scenarios where we want to add certain types to a collection safely. Can anyone think of a use case?
We might be adding items to a collection that only accepts certain types, like adding objects to a list during runtime?
Spot on! So always remember: Consumer Super for lower bounded wildcards. Great work, class!
To wrap up our discussion on wildcards, let’s summarize the PECS rule. Who can explain what PECS stands for?
Producer Extends, Consumer Super!
Exactly! It is a mnemonic to help us remember when to use upper and lower bounded wildcards. If you're producing (reading), you use extends, and if you're consuming (writing), you use super.
So if I want to create a method that reads from a structure, I should consider upper bounded wildcards?
That’s correct! Likewise, when you’re adding items, think lower bounded. Can anyone give me an example of when to use each?
Reading a list of numbers would be upper, and adding to a list of objects would be lower!
Very well done! Understanding the PECS rule will greatly improve your ability to use generics in Java.
Allows for any type, denoted as <?>.
Represented as <? extends T>, useful for reading items of type T or its subtypes.
Denoted as <? super T>, designed for writing items of type T or its supertypes.
A mnemonic for remembering when to use upper or lower bounded wildcards based on production or consumption.
Example of Unbounded Wildcard: public void process(List<?> list) allows processing any type of list.
Example of Upper Bounded Wildcard: public void printNumbers(List<? extends Number> numbers) accepts lists of Numbers, Integers, or Doubles.
Example of Lower Bounded Wildcard: public void addToList(List<? super Integer> list) allows adding integers to any list that can hold Integer or its super types.
When reading types, extend is the key, when writing types, super’s the plea.
Imagine a library where books of different genres are organized. When you want to borrow books (read), you can pick any genre (unbounded). But if you want to donate books (write), you must ensure they fit a category (lower bounded) or can cater to all readers (upper bounded).
PECS: Producers Extend, Consumers Super - where producers crave reading, and consumers crave writing.
PEC: Producer-Extend, Consumer-Super to remember the rules of wildcards.
Term
What does an unbounded wildcard (`<?>`) indicate?
Click to flip
Term
What is the purpose of an upper bounded wildcard?
Click to flip
Term
When would you use a lower bounded wildcard?
Click to flip
Term
What does the PECS rule help you remember?
Click to flip
A wildcard in generics represented as <?> that allows any type and provides flexibility.
A wildcard represented as <? extends T> that allows reading items of type T or its subtypes.
A wildcard denoted as <? super T> that allows writing items of type T or its supertypes.
A mnemonic for deciding between upper and lower bounded wildcards: Producer Extends, Consumer Super.
This section covers the concept of wildcards in Java generics, detailing their types and uses for type safety.
Wildcards in Java generics allow for flexibility in type specification. They include unbounded, upper-bounded, and lower-bounded wildcards, each having distinct purposes. Understanding these wildcards is crucial for developer efficiency and avoiding casting errors.
Generics in Java allow for type-safe code and enhance code reusability. Wildcards are a way to specify a type in generics when the exact type is unknown, providing three main types:
Unbounded Wildcards (<?>): Useful when the type is not specified. It allows for flexibility when you do not require any specific type constraints. For instance, a method could accept any type of List<?>.
Upper Bounded Wildcards (<? extends T>): This form allows reading from a structure that can be of type T or any subtype of T. It is beneficial when you want to ensure that you can retrieve elements of a certain type or its subtypes. For example, a method parameter of List<? extends Number> can accept List<Integer> or List<Double>.
Lower Bounded Wildcards (<? super T>): This wildcard is for writing to a structure that can be of type T or any supertype of T. This is useful when you need to add elements of a specific type safely. For instance, a parameter of List<? super Integer> will accept a list of Integer or any supertype, such as Number or Object.
Additionally, the PECS rule succinctly encapsulates how to know which wildcard to use: Producer Extends, Consumer Super. This principle helps clarify when to use upper or lower bounded wildcards based on whether you are producing or consuming elements from a collection.