Week 1 Flashcards
Which of the following describes the benefits of using Java Generics? Select all that apply.
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.
defines a class to hold a pair of values using Java Generics?
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; }
}