Lecture 8A Flashcards

1
Q

A generic class name should always be noted together with…

A

…the ie MyClass

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

Java compiler can show you error with generics, when?

A

Before runtime.

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

What are benefits of using generics?

A

Increase type safety. Avoid type casts and code duplication. Also many runtime errors become compile time errors, checked statically.

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

What are type bounds?

A

They restrict range of type parameters. Ie class myGen… This means any type T must extend OR implement type Sup.

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

What is type erasure?

A

When the Java compiler translates generics into ordinary code after type checking.

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

In generics what is a wildcard?

A

? Is a wildcard. Used in generics in methods: void myMethod(MyGenericClass> arg)
It stands for unknown type.
Can also use bounds: extends, super. Can also be used for return values.

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

What is convariance?

A

Convariant arrays would be where Employee is a subclass of Person, Person[] array = new Employee[5]

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

Why are Java generics not convariant?

A

To avoid runtime issues, ie Person[] array = new Emp[5]; array[0] = new Student(); compiles fine but error at runtime.

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

Java generics are invariant?

A

Yes, ArrayList is NOT a subclass of ArrayList.

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

What are generic static methods?

A

They use the static keyword and type parameters (not the wildcard ?): static void methodName(T[ ] a, Collection c){…}

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

How can generic static methods be used?

A

Regardless of whether the class in which it’s declared is generic. Also, when a generic method is used it is NOT required to provide a concrete type for type parameter.

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

What is the type parameter? And type argument? And type variable?

A

class MyClass {}

When making object from class:
MyClass myO = new MyClass(); String here is type argument. Inside class typeparameter is a type variable.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly