CS 213 Fall 2021 Note #17 Generic Programming in Java Dr. Generic Class
We want to write Java classes that have general applicability. For example, a generic Stack, or a generic Bag that can hold different object types. Generics also enable you to detect errors at compile time rather than at runtime. You have used a generic interface Comparable in the programming assignment. Generics let you parameterize types. With this capability, you can define a class or a method with generic types that the compiler can replace with concrete types.
The key benefit of generics is to enable errors to be detected at compile time rather than at runtime. A generic class or method permits you to specify allowable types of objects that the class or method can work with. If you attempt to use an incompatible object, the compiler will detect that error.
package java.lang;
Copyright By Assignmentchef assignmentchef
public interface Comparable
Here,
The statement in (b) declares that c is a reference variable whose type is Comparable and invokes the compareTo method to compare a Date object with a string. This code has a compile error because the argument passed to the compareTo method must be of the Date type. Since the errors can be detected at compile time rather than at runtime, the generic type makes the program more reliable.
Generic types must be reference types. You cannot replace a generic type with a primitive type such as int, double, or char. You need to use the wrapper classes in this case; for example, Integer class. A generic type can be defined for a class or interface. A concrete type must be specified when using the class to create an object or using the class or interface to declare a reference variable.
public class GenericStack
private java.util.ArrayList
Creating a stack to hold strings:
GenericStack
To define a generic type for a class, place the
CS 213 Fall 2021 Note #17 Generic Programming in Java Dr. As another example, ArrayList class is a container class in the Java Collections; it is a generic class.
The statement: ArrayList
ArrayList
you can only add Employee objects to the list. As a result, casting is not needed to retrieve a value from a list with a specified element type because the compiler already knows the element type. If the elements are of wrapper types, such as Integer, Double, and Character, you can directly assign an element to a primitive-type variable. This is called auto unboxing. For example,
ArrayList
list.add(5.5); // 5.5 is automatically converted to new Double(5.5) list.add(3.0); // 3.0 is automatically converted to new Double(3.0) Double doubleObject = list.get(0); // No casting is needed double d = list.get(1); // Automatically converted to double
Instead of using a generic type, you could simply make the type element Object, which can accommodate any object type. However, using a specific concrete type can improve software reliability and readability because certain errors can be detected at compile time rather than at runtime. As in the previous example, since stack1 is declared as a String type with the GenericStack class, only strings can be added to the stack. It would be a compile error if you attempt to add an integer to the stack1.
To create a stack of strings, you use new GenericStack
public GenericStack
public class ArrayBag
private E[] elements;
private int num;
public ArrayBag() { //constructor
elements = (E[]) new Object[MAXSIZE];
num = 0; }
CS 213 Fall 2021 Note #17 Generic Programming in Java Dr. Generic Method
A generic type can be defined for a static method. A generic method is written with the types of the parameters not fully specified. For example, the following code segment defines a generic method print to print an array of objects.
public class GenericMethodDemo {
public static void main(String[] args ) {
Integer[] integers = {1, 2, 3, 4, 5};
String[] strings = {London, Paris, , Austin}; GenericMethodDemo.
public static
static Integer middle(Integer [] data) static Character middle(Character [] data)
The only difference of the above statements is the type. We can write a generic method with the types of the parameters not fully specified:
static
static boolean most(S[] s, S skey, T[] t, T tkey)
To invoke a generic method, prefix the method name with the actual type in angle
brackets. More example:
CS: assignmentchef QQ: 1823890830 Email: [email protected]
Reviews
There are no reviews yet.