<< Chapter < Page | Chapter >> Page > |
If that has happened to you, you have had your first encounter with a new feature in Java version 1.5 known as generics.
What are generics?
Oracle has this to say about generics (boldface added for emphasis):
"This long-awaited enhancement to the type system allows a type or method to operate on objects of various types while providing compile-time type safety . It adds compile-time type safety to the Collections Framework and eliminates the drudgery of casting."
In addition to the Collections Framework, generics impacts several other areas of Java programming as well.
Generics are not particularly easy
Don't be lulled into a false sense of security by what you will find in this module. Although the introductory material presented in this module israther straightforward, a full understanding of generics can be fairly difficult. The tentacles of generics reach into many different areas ofJava in very complex ways.
The truth of this is borne out by the fact that the excellent book entitled Java How to Program, Sixth Edition (Deitel) dedicates an entire chapter consisting of more than 30 pages to generics. Furthermore, the chapter ongenerics doesn't even include many additional pages that the book dedicates to a discussion of the impact of generics on the Java Collections Framework.The book covers several topics in the chapter on generics, including the following:
The impact of generics on the Java Collections Framework is covered in a different chapter. Hopefully, the benefits provided by generics will outweighthe additional complexity. Only time will tell.
The Java Collections Framework
Even though the material that will be presented in this module will be relatively straightforward, you will need to know something about the JavaCollections Framework for it to make much sense. If you need to learn more about the Java Collections Framework, see the modules beginning with Java4010: Getting Started with Java Collections at I TSE 2321 - Object-Oriented Programming (Java) . Be aware, however, that those modules were originally published long before generics existed in Java. If youcompile the programs in those modules using Java version 1.5 or a later version, you will get the warning messages from the compiler shown in Figure 1 .
I will present and explain several very simple programs in this module. Those programs are intended to illustrate the following concepts:
Notification Switch
Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?