<< Chapter < Page Chapter >> Page >

(Note the code in Listing 7 that shows the type Date enclosed in matching angle brackets. This is the primarysyntax change required to use generics.)

What does this syntax mean?

One way to think of this syntax is that the expression on the right of the assignment operator instantiates a new ArrayList object that is capable of containing only references to objects of type Date .

(Those references are probably still stored as type Object . We will see how this apparent discrepancy is reconciled later through automaticcasting.)

Similarly, the expression on the left of the assignment operator is the declaration of an instance variable capable of holding a reference to an ArrayList object, which in turn is capable of containing only references to objects oftype Date .

Must qualify both expressions

Additional changes to generics were made in the release of Java version 1.7. Prior to the release of Java version 1.7, it was necessary to qualify the expressions on both sides of the assignmentoperator by use of the angle-bracket syntax. If the qualifier was included in the expression on the right, but was omitted from the variable declaration onthe left, the compilation would fail later. That is still true following the release of Java version 1.7.

If the angle-bracket qualifier was included with the variable declaration on the left and omitted from the instantiation of the new object on the right, theprogram would compile and run successfully. However, the compiler would issue an unchecked conversion warning indicating the possibility of a runtime error under certain conditions.

Following the release of Java version 1.7, it is not necessary to qualify the expression on the right side of the assignment operator. You will learn moreabout this in a future module.

What does Oracle have to say?

According to Oracle (boldface added for emphasis),

"Generics provides a way for you to communicate the type of a collection to the compiler, so that it can be checked. Once the compilerknows the element type of the collection, the compiler can check that you have used the collection consistently and can insert the correct casts on values being taken out of the collection."

Note the boldface text in the above quotation indicating that the compiler modifies your code by inserting casts where appropriate.

Compile-time type safety

In discussing code similar to the code in Listing 7 , Oracle states,

"... so the compiler can verify at compile time that the type constraints are not violated at run time. Because the program compileswithout warnings, we can state with certainty that it will not throw a ClassCastException at run time. The net effect of using generics, especially in large programs, is improved readability and robustness."

In order to achieve compile-time type safety, it is necessary that the program compiles without warnings. Otherwise, the program may execute, butmay throw a ClassCastException at runtime.

More on generics with collections

In further explaining generics as used with collections, Oracle goes on to say (boldface added for emphasis):

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