Advanced Features in Java: Wildcards and Type Capture


Overview/Description
Expected Duration
Lesson Objectives
Course Number
Expertise Level



Overview/Description

In this course, you'll explore how wildcards and type capture work, as well as how type erasure manifests itself in compiled bytecode. You'll start by examining how the wildcard operator, represented in syntax by the ? character, can be used in situations where you don't know up-front what type argument to specify for a particular type parameter. Next, you'll learn the three types of wildcard type parameters. Unbounded wildcards, where you know nothing at all about the type; upper bounded wildcards, which define type parameters that must inherit from a specific base class or interface; and finally lower bounded wildcards, which can be used to specify constraints in the other direction - namely that the type must be a super-class of a specified type. Next, you'll see that capture errors occur when Java is unable to infer the correct type to pencil in as the type argument for a given type parameter. Finally, you'll examine bytecode to see that whenever you create a class or method with a type parameter, Java creates just one single copy of such code, and pencils in a type parameter of java.lang.Object. It also adds various type checks to ensure that the code is used correctly. This is known as type erasure and forms the basis of the great performance of Java generics.



Expected Duration (hours)
1.5

Lesson Objectives

Advanced Features in Java: Wildcards and Type Capture

  • discover the key concepts covered in this course
  • recognize how you can constrain type parameters to extend the Comparable and Serializable interfaces
  • demonstrate various relationships between base and derived classes in the presence of type parameters
  • recognize how type parameters may exist only in the base class, or only in the derived class, or in both
  • recognize use-cases where a base class specifies one type parameter and the derived class specifies multiple type parameters
  • describe how wildcards mitigate some limitations of invoking methods with generic input arguments
  • use upper bounded wildcards to specify that a type parameter must be a sub-class of a certain type
  • recognize how unbounded wildcards can be used where no information about inheritance relationships is available
  • use lower bounded wildcards to specify that a type parameter must be a super-class of a certain type
  • recognize scenarios where capture errors might occur and some potential fixes for such capture errors
  • analyze compiled bytecode to see how Java uses type erasure to ensure type safety and prevent code bloat
  • summarize the key concepts covered in this course
  • Course Number:
    it_jdadfjdj_10_enus

    Expertise Level
    Expert