What is wildcards (?) in Java?

Wildcards in Java are used in generics to signify an unknown type. They allow for more flexible coding when working with collections and data structures. Wildcards can be categorized into three types:

  • Unbounded Wildcards: Represented by the question mark (?) and can accept any type.
  • Upper Bounded Wildcards: Denoted by ? extends ClassName, allowing for the use of a specified class or its subclasses.
  • Lower Bounded Wildcards: Indicated by ? super ClassName, allowing for the use of a specified class or its superclasses.

Using wildcards can help in writing more general and reusable code. When you don't know what parameter type to expect, wildcards can be a useful tool.


Java wildcards generics in Java unknown type in Java upper bounded wildcards lower bounded wildcards