<< 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:

  • Generic methods
  • Generic classes
  • Raw types
  • Wildcards
  • Generics and inheritance

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 .

Preview

I will present and explain several very simple programs in this module. Those programs are intended to illustrate the following concepts:

  • How the Java Collections Framework behaved prior to the release of Javaversion 1.5.
  • The effect of an incorrect cast in code that doesn't use generics.
  • Avoiding the requirement to cast through the use of generics. This program also includes an illustration of some of the required syntax forgenerics.
  • Compile-time type safety provided by the use of generics.
  • Syntax requirements for the use of iterators with generics. This program also illustrates the use of the enhanced for loop with collections, which is another new feature in Java version 1.5.

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Object-oriented programming (oop) with java. OpenStax CNX. Jun 29, 2016 Download for free at https://legacy.cnx.org/content/col11441/1.201
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?

Ask