Chapter 21 - Generics Flashcards

1
Q

What is generics?

A

Generics are not objects. Generics is a concept/idea. You can define generic classes, interfaces and methods. Generics lets you parameterize types.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Why use generics?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is “generic instantiation”?

A
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<>();
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

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)

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

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?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How do you define a generic class?

A
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);
-   }
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is special about the constructor of generic classes?

A

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()”.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Can a generic class take more than one generic type parameter?

A
Yes. In this case, place the parameters together inside the brackets, separated by commas. Example:
.class GenericStack[E, F, G>
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

The String class implements the interface Comparable. How does the class header of String look like?

A

public class String implements Comparable[T>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How do you define a generic type for a method?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How do you invoke a generic method?

A

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);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is a bounded generic type?

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is a raw type?

A
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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Wildcard generic types. What are unbounded wildcards, bounded wildcards, and lower-bound wildcards?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

There are 4 general restrictions on how generic types can be used. What are they?

A
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.
  1. 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Which of the following statements is correct?

A. Generics can help detect type errors at compile time, thus make programs more robust.
B. Generics can make programs easy to read.
C. Generics can avoid cumbersome castings.
D. Generics can make programs run faster.

A

All but D are true

17
Q

Which of the following statements is correct?

A. Comparable[String> c = new String("abc");
B. Comparable[String> c = "abc";
C. Comparable[String> c = new Date();
D. Comparable[Object> c = new Date();
A

A and B are correct

18
Q

Suppose List list = new ArrayList(). Which of the following operations are correct?

A. list.add(“Red”);
B. list.add(new Integer(100));
C. list.add(new java.util.Date());
D. list.add(new ArrayList());

A

All are correct, becase list takes in Object type objects. When reading them out, casting is needed.

19
Q
The method header is left blank in the following code. Fill in the header.
public class GenericMethodDemo {
  public static void main(String[] args ) {
    Integer[] integers = {1, 2, 3, 4, 5};
    String[] strings = {"London", "Paris", "New York", "Austin"};
print(integers);
print(strings);   }

___________ {
for (int i = 0; i void print(E[] list)

A

D and E are correct

20
Q

Which of the following declarations use raw type?

A. ArrayList[Object> list = new ArrayList[Object>(); 
B. ArrayList[String> list = new ArrayList[String>(); 
C. ArrayList[Integer> list = new ArrayList[Integer>(); 
D. ArrayList list = new ArrayList();
A

D is correct

21
Q

Is ArrayList[Integer> a subclass of ArrayList[Object>?

A. Yes
B. No

A

B. No

22
Q

Is ArrayList[Integer> a subclass of ArrayList[?>?

A. Yes
B. No

A

A. Yes

23
Q

Is ArrayList[Integer> a subclass of ArrayList[? extends Number>?

A. Yes
B. No

A

A. Yes

24
Q

Does [? super Number> represent a superclass of Number?

A. Yes
B. No

A

A. Yes

25
Q

Which of the following statements are true?

A. Generic type information is present at compile time.
B. Generic type information is not present at runtime.
C. You cannot create an instance using a generic class type parameter.
D. You cannot create an array using a generic class type parameter.
E. You cannot create an array using a generic class.
A

All are true