<< 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:
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
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 .
Notification Switch
Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?