Generics Flashcards
What is generics?
Generics are not objects. Generics is a concept/idea. You can define generic classes, interfaces and methods. Generics lets you parameterize types.
Why use generics?
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.
What is “generic instantiation”?
The act of replacing a generic type with an actual concrete type is called generic instantiation. Example: The class ArrayList with the generic type E can be instantiated like this: ArrayList strList = new ArrayList();
Analyze the following code:
ArrayList[int> intList = ArrayList();
intList.add(3);
(There should be angle brackets on both sides of “int”, but Brainscape is buggy and won’t allow it)
You cannot replace a generic type with a primitive type such as int, double, or char. To create an array list for int values, you have to use the Integer wrapper class:
ArrayList[Integer> intList = ArrayList();
You CAN add int values to this intList, because java autoboxes int values to Integer objects.
Consider the following code:
ArrayList[String> list = new ArrayList();
list.add(“Red”);
String s = // list.get(0);
Do you have to cast list.get(0) into String?
Consider the following code:
ArrayList[Integer> list = new ArrayList(); list.add(3); int number = // list.get(0);
Do you have to cast list.get(0) into int?
In the first example, Java knows that the elements in list are String objects, so no casting is needed.
In the second example, Java converts Integer objects into int values automatically. This is a language feature called autounboxing.
How do you define a generic class?
You add a generic type name in angle brackets at the end of the class name. Example: class GenericStack[E> { - private ArrayList[E> list = - new ArrayList(); - void add(E o) { - list.add(o); - } }
What is special about the constructor of generic classes?
Nothing really. The constructor does not have the angled blackets. Consider the class “public class GenericStack[E>”. The constructor of this class would be “public GenericStack()”.
Can a generic class take more than one generic type parameter?
Yes. In this case, place the parameters together inside the brackets, separated by commas. Example: .class GenericStack[E, F, G>
The String class implements the interface Comparable. How does the class header of String look like?
public class String implements Comparable[T>
How do you define a generic type for a method?
Generic types may be defined for static methods. To declare a generic method, you place the generic type immediately after the keyword static in the method header.
Example:
public static [E> void print(E[] list)
How do you invoke a generic method?
To invoke a generic method, prefix the method name with the actual type in angle brackets.
Example:
GenericMethodDemo.[Integer>print(integers);
GenericMethodDemo.[String>print(strings);
What is a bounded generic type?
A bounded generic type is a generic type that is specified as a subtype of another type. The syntax is: [T extends superType>.
Example method header:
public static [T extends superType> boolean equalArea(E object1, E object2)
What is a raw type?
A generic class such as GenericStack and ArrayList used without a type parameter is called a raw type. Using raw types allows for backward compatibility with earlier versions of Java.
Wildcard generic types. What are unbounded wildcards, bounded wildcards, and lower-bound wildcards?
Unbounded wildcard: [?>
Bounded wildcard: [? extends T>
Lower-bound wildcard: [? super T>
In generic code, the question mark (?), called the wildcard, represents an unknown type. The wildcard can be used in a variety of situations: as the type of a parameter, field, or local variable; sometimes as a return type (though it is better programming practice to be more specific). The wildcard is never used as a type argument for a generic method invocation, a generic class instance creation, or a supertype.
There are 4 general restrictions on how generic types can be used. What are they?
1. Cannot use "new E()" You cannot create an instance using a generic type parameter. The reason is that "new E()" is executed at runtime, but the generic type E is not available at runtime.
- Cannot use “new E[]”
You cannot create an array using a generic type parameter.
3. A generic Type Parameter of a Class Is Not Allowed in a Static Context Since all instances of a generic class have the same runtime class, the static variables and methods of a generic class are shared by all its instances. Therefore, it is illegal to refer to a generic tpy parameter for a class in a static method, field, or initializer.
4. Exception Classes Cannot Be Generic A generic class may not extend java.lang.Throwable