Documentation

The Java™ Tutorials
Hide TOC
Generics (Updated)泛型(更新版)
Trail: Learning the Java Language

Lesson: Generics (Updated)课程:泛型(更新)

In any nontrivial software project, bugs are simply a fact of life.在任何非平凡的软件项目中,bug只是生活中的一个事实。 Careful planning, programming, and testing can help reduce their pervasiveness, but somehow, somewhere, they'll always find a way to creep into your code.仔细的规划、编程和测试有助于减少它们的普遍性,但不知何故,在某些地方,它们总会找到一种方法潜入到您的代码中。This becomes especially apparent as new features are introduced and your code base grows in size and complexity.随着新特性的引入以及代码库的大小和复杂性的增加,这一点变得尤为明显。

Fortunately, some bugs are easier to detect than others.幸运的是,有些bug比其他bug更容易检测。Compile-time bugs, for example, can be detected early on; you can use the compiler's error messages to figure out what the problem is and fix it, right then and there.例如,编译时错误可以在早期检测到;您可以使用编译器的错误消息来找出问题所在并立即修复它。Runtime bugs, however, can be much more problematic; they don't always surface immediately, and when they do, it may be at a point in the program that is far removed from the actual cause of the problem.然而,运行时bug的问题要大得多;它们并不总是立即出现,当它们出现时,可能是在程序中与问题的实际原因相去甚远的地方。

Generics add stability to your code by making more of your bugs detectable at compile time.泛型通过在编译时检测更多的bug来增加代码的稳定性。After completing this lesson, you may want to follow up with the Generics tutorial by Gilad Bracha.完成本课程后,您可能希望继续学习Gilad Bracha的泛型教程。


Previous page: Previous Lesson
Next page: Why Use Generics?