Java教程
  • Introduction
  • Getting Started
    • The Java Technology Phenomenon
      • About the Java Technology
      • What Can Java Technology Do?
      • How Will Java Technology Change My Life?
    • The Hello World Application
    • A Closer Look at the Hello World Application
  • Learning the Java Language
    • Object-Oriented Programming Concepts
      • What Is an Object?
      • What Is a Class?
      • What Is Inheritance?
      • What Is an Interface?
      • What Is a package?
    • Language Basics
      • Java Language Keywords
    • Annotations
      • Annotations Basics
      • Declaring an Annotation Type
      • Predefined Annotation Types
      • Repeating Annotations
      • Type Annotations and Pluggable Type Systems
    • Generics
      • Why Use Generics?
      • Generic Types
        • Raw Types
      • Generic Methods
      • Bounded Type Parameters
        • Generic Methods and Bounded Type Parameters
      • Generics, Inheritance, and Subtypes
      • Type Inference
      • Wildcards
        • Upper Bounded Wildcards
        • Unbounded Wildcards
        • Lower Bounded Wildcards
        • Wildcards and Subtyping
        • Wildcard Capture and Helper Methods
        • Guidelines for Wildcard Use
      • Type Erasure
        • Erasure of Generic Types
        • Erasure of Generic Methods
        • Effects of Type Erasure and Bridge Methods
        • Non-Reifiable Types
      • Restrictions on Generics
Powered by GitBook
On this page

Was this helpful?

  1. Learning the Java Language
  2. Generics

Type Erasure

泛型的引入为Java语言提供了泛型编程的支持并且使得编译时有了更严格的类型检查。为了实现泛型,Java编译器使用了类型擦除:

  • 将泛型类型的所有类型参数替换为他们的边界或者是Object(如果类型参数没有边界)。也就是说,产出的字节码只包含原始的类,接口以及方法。

  • 必要时插入类型转换以保持类型安全。

  • 生成桥接函数以便在扩展的泛型类型中保留多态。

类型擦除可确保不会为参数化类型而创建新的类;因此,泛型不会产生运行时开销。

PreviousGuidelines for Wildcard UseNextErasure of Generic Types

Last updated 5 years ago

Was this helpful?