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

Questions & Answers

what is phylogeny
Odigie Reply
evolutionary history and relationship of an organism or group of organisms
AI-Robot
ok
Deng
what is biology
Hajah Reply
the study of living organisms and their interactions with one another and their environments
AI-Robot
what is biology
Victoria Reply
HOW CAN MAN ORGAN FUNCTION
Alfred Reply
the diagram of the digestive system
Assiatu Reply
allimentary cannel
Ogenrwot
How does twins formed
William Reply
They formed in two ways first when one sperm and one egg are splited by mitosis or two sperm and two eggs join together
Oluwatobi
what is genetics
Josephine Reply
Genetics is the study of heredity
Misack
how does twins formed?
Misack
What is manual
Hassan Reply
discuss biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles
Joseph Reply
what is biology
Yousuf Reply
the study of living organisms and their interactions with one another and their environment.
Wine
discuss the biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles in an essay form
Joseph Reply
what is the blood cells
Shaker Reply
list any five characteristics of the blood cells
Shaker
lack electricity and its more savely than electronic microscope because its naturally by using of light
Abdullahi Reply
advantage of electronic microscope is easily and clearly while disadvantage is dangerous because its electronic. advantage of light microscope is savely and naturally by sun while disadvantage is not easily,means its not sharp and not clear
Abdullahi
cell theory state that every organisms composed of one or more cell,cell is the basic unit of life
Abdullahi
is like gone fail us
DENG
cells is the basic structure and functions of all living things
Ramadan
What is classification
ISCONT Reply
is organisms that are similar into groups called tara
Yamosa
in what situation (s) would be the use of a scanning electron microscope be ideal and why?
Kenna Reply
A scanning electron microscope (SEM) is ideal for situations requiring high-resolution imaging of surfaces. It is commonly used in materials science, biology, and geology to examine the topography and composition of samples at a nanoscale level. SEM is particularly useful for studying fine details,
Hilary
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

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