<< Chapter < Page Chapter >> Page >

"... when we declare c to be of type Collection (String in angle brackets), thistells us something about the variable c that holds true wherever and whenever it is used, and the compiler guarantees it (assuming the program compiles without warnings ). A cast, on the other hand, tells us something the programmer thinks is true at a single point in the code, andthe VM checks whether the programmer is right only at run time."

The bottom line

The bottom line on generics (when used with collections) seems to be that references to objects are still stored in the collection as type Object . However, when we notify the compiler of the type of data to be stored in thecollection using angle-bracket syntax, and the program compiles without warnings, the compiler will do at least the following:

  • Ensure that only references to objects of the specified type are stored in the collection, and used consistently throughout the program, thuseliminating the possibility of a ClassCastException at runtime.
  • Automatically cast the reference to the specified type when it is later retrieved by program code.

No explicit cast is required

That brings us back to a discussion of the code in Listing 7 . Note that unlike the code in Listing 5 , the print statement in Listing 7 does not contain an explicit cast to type Date , (at least not in the code that I wrote).

As described above, having been notified that the collection can contain only references to objects of type Date , the compiler automatically inserted a cast to type Date at the appropriate place in the code, therebyguaranteeing that the reference is converted from type Object to type Date before the getTime method is called on the reference.

There is still a cast involved. However, the cast is automatically inserted by the compiler. This eliminates the requirement for me (the programmer ) to insert the cast, and also eliminates the possibility of me inserting an incorrect cast.

Once again, all of this assumes that the program compiles without warnings.

Program output

The program in Listing 7 compiles and executes correctly, producing the following output for one particular run.

1377995720768

Compile-time type safety provided by the use of generics

The program named Generics04 shown in Listing 8 illustrates the ability of generics to prevent the storing of the wrong type of references in a collection. This in turn can preventruntime errors.

A new ArrayList object

Listing 8 instantiates a new object of type ArrayList capable of storing only references to objects of type Date . This object's reference is stored in the instance variable named var1 .

Other types cannot be stored in the collection

Once the ArrayList has been constrained to contain only references to objects of type Date , the compiler will not allow a reference to an object of any other type (other than types that are assignment compatible with Date , such as subclasses of Date ) to be stored in the collection.

A compiler error rather than a runtime error

The first statement in the runIt method in Listing 8 attempts to add a new element to the ArrayList object. The new element is a reference to a literal String object that encapsulates the string "abcd". This results in the compiler error shown in Figure 4 .

Questions & Answers

how did the oxygen help a human being
Achol Reply
how did the nutrition help the plants
Achol Reply
Biology is a branch of Natural science which deals/About living Organism.
Ahmedin Reply
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
cell is the smallest unit of the humanity biologically
Abraham
what is biology
Victoria Reply
what is biology
Abraham
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
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