Chapter 10.1.3 Generic Programming in Java | Introduction to Programming Using Java

Chapter 10.1.3  Generic Programming in Java | Introduction to Programming Using Java

 

10.1.3  Generic Programming in Java

 

Java’s generic programming features have gone through several stages of development. The original version of Java had just a few generic data structure classes, such as Vector, that could hold values of type Object. Java version 1.2 introduced a much larger group of generics that followed the same basic model. These generic classes and interfaces as a group are known as the Java Collection Framework. The ArrayList class is part of the Collection Framework.

 

Chapter 10.1.3 Generic Programming in Java | Introduction to Programming Using Java

 

The original Collection Framework was closer in spirit to Smalltalk than it was to C++, since a data structure designed to hold Objects can be used with objects of any type. Unfortunately, as in Smalltalk, the result is a category of errors that show up only at run time, rather than at compile time. If a programmer assumes that all the items in a data structure are strings and tries to process those items as strings, a run-time error will occur if other types of data have inadvertently been added to the data structure.

In Java, the error will most likely occur when the program retrieves an Object from the data structure and tries to type-cast it to type String. If the object is not actually of type String, the illegal type-cast will throw an error of type ClassCastException.

Java 5.0 introduced parameterized types, such as ArrayList<String>. This made it possible to create generic data structures that can be type-checked at compile time rather than at run time. With these data structures, type-casting is not necessary, so ClassCastExceptions are avoided.

 

Chapter 10.1.3 Generic Programming in Java | Introduction to Programming Using Java

 

The compiler will detect any attempt to add an object of the wrong type to the data structure; it will report a syntax error and will refuse to compile the program. In Java 5.0, all of the classes and interfaces in the Collection Framework, and even some classes that are not part of that framework, have been parameterized.

Java’s parameterized classes are similar to template classes in C++ (although the implementation is very different), and their introduction moves Java’s generic programming model closer to C++ and farther from Smalltalk.

In this chapter, I will use the parameterized types almost exclusively, but you should remember that their use is not mandatory. It is still legal to use a parameterized class as a non-parameterized type, such as a plain ArrayList.

Note that there is a significant difference between parameterized classes in Java and template classes in C++. A template class in C++ is not really a class at all—it’s a kind of factory for generating classes. Every time the template is used with a new type, a new compiled class is created. With a Java parameterized class, there is only one compiled class file. For example, there is only one compiled class file, ArrayList.class, for the parameterized class ArrayList.

 

Chapter 10.1.3 Generic Programming in Java | Introduction to Programming Using Java

 

The parameterized types ArrayList<String> and ArrayList<Integer> both use the some compiled class file, as does the plain ArrayList type. The type parameter—String or Integer—just tells the compiler to limit the type of object that can be stored in the data structure. The type parameter has no effect at run time and is not even known at run time. The type information is said to be “erased” at run time. This type erasuer introduces a certain amount of weirdness.

For example, you can’t test “if (list instanceof ArrayList<String>)” because the instanceof operator is evaluated at run time, and at run time only the plain ArrayList exists. Even worse, you can’t create an array that has base type ArrayList<String> by using the new operator, as in “new ArrayList<String>[N]”. This is because the new operator is evaluated at run time, and at run time there is no such thing as “ArrayList<String>”; only the non-parameterized type ArrayList exists at run time.

 

Chapter 10.1.3  Generic Programming in Java | Introduction to Programming Using Java

 

Fortunately, most programmers don’t have to deal with such problems, since they turn up only in fairly advanced programming. Most people who use the Java Collection Framework will not encounter them, and they will get the benefits of type-safe generic programming with little difficulty.

 

SEE MORE:

15 thoughts on “Chapter 10.1.3 Generic Programming in Java | Introduction to Programming Using Java”

Leave a Comment