Week 1 Flashcards

1
Q

Which of the following describes the benefits of using Java Generics? Select all that apply.

A

They allow the user of a class to specify the datatype that the class will use internally. Java Generics use a type parameter as a placeholder for a specific datatype , which is specified by the caller when an instance of the generic class is created.

They allow for the creation of a generalized class that do not require casting of method return values. Could create a generalized class that uses Objects, but callers would need to cast method return values.

They can reduce the amount of duplicated (or very similar) code in a system. Instead of creating classes that are nearly identical, we can use Generics to create a generalized implementation.

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

defines a class to hold a pair of values using Java Generics?

A

public class Pair {

	private T one, two;
	public Pair(T t1, T t2) {
		one = t1;
		two = t2;
	}
	public T getOne() { return one; }
	public T getTwo() { return two; }

}

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