0% found this document useful (0 votes)
19 views3 pages

Chapter 21 Generics: Package Public Interface Public Int

1. Generics allow types (classes and interfaces) to be parameters when defining classes, interfaces and methods. This allows type-safe operations on objects of various types. 2. Using generics improves type safety by catching errors at compile time that would otherwise only be caught at runtime on raw types. 3. When declaring a generic type, the type parameter follows the class or method name enclosed in angle brackets <>. Bounded and wildcard types can also be used to restrict the possible types.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views3 pages

Chapter 21 Generics: Package Public Interface Public Int

1. Generics allow types (classes and interfaces) to be parameters when defining classes, interfaces and methods. This allows type-safe operations on objects of various types. 2. Using generics improves type safety by catching errors at compile time that would otherwise only be caught at runtime on raw types. 3. When declaring a generic type, the type parameter follows the class or method name enclosed in angle brackets <>. Bounded and wildcard types can also be used to restrict the possible types.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 3

Chapter 21 Generics

1. (a) will compile fine, but (b) has a compilation error on Line 3, because dates is
declared as a list of Date objects. You cannot assign a string to the list.

2. casting is needed in (a), but no casting is necessary in (b) with the generic type
ArrayList<Date>.

3. One important benefit is improving reliability and robustness. Potential errors can be
detected by the compiler.

4.

package java.lang;

public interface Comparable<E> {


public int compareTo(E o)
}

5. No.

6. Yes.

7. To declare a generic type for a class, place the generic type after the class name, such
as GenericStack<E>. To declare a generic type for a method, place the generic type
for the method return type, such as <E> void max(E o1, E o2).

8. To declare a generic method, you place the generic type <E> immediately after the
keyword static in the method. A generic method can be invoked just like a regular
method. The compiler automatically discovers the actual type.

9. Bounded generic type such as <E extends AClass> specifies that a generic type must
be a subclass of AClass.

10. No, because list is of type int[], but the sort method requires E[], where E is an
object type.

11. No, because list is still of type int[], but the sort method requires E[], where E is
an object type.

12. When you use generic type without specifying an actual parameter, it is called a
raw type. A raw type is unsafe, because some errors cannot be detected by the
compiler. The raw type is allowed in Java for backward compatibility.

13. ArrayList list = new ArrayList();


14. GenericStack is roughly equivalent to GenericStack<Object>, but they are not the
same. GenericStack<Object> is a generic instantiation, but GenericStack is a raw
type.

15. ? is unbounded wildcard


? extends T is bounded wildcard
? super T is lower bounded wildcard

16. The program cannot be compiled, because the element type in stack1 is
GenericStack<String>, but the element type is stack2 is GenericStack<Object>.
add(stack1, stack2) cannot be matched.

17. The program can be compiled and run fine.

18. Generic type information is used by the compiler to check whether the type is
used safely. Afterwards the type information is erased. The type information is not
available at runtime. This approach enables the generic code to
be backward-compatible with the legacy code that uses raw
types.

19. No. Only ArrayList is loaded.

20. No, because the type information is not available at runtime.

21. Since all instances of a generic class have the same


runtime class, the static variables and methods of a
generic class is shared by all its instances. Therefore,
it is illegal to refer a generic type parameter for a
class in a static method or initializer.

22. No. The JVM have to check the exception thrown from the try clause to see if it
matches the type specified in a catch clause. This is impossible, because the type
information is not present at runtime.

23. Because these methods cannot be implemented in the GenericMatric class.

24. In the IntegerMatrix class, the add method is implemented by adding the two
numbers using the + operator. The multiply method is implemented by multiplying
the two numbers using the * operator. The zero method is implemented to return 0.

25. In the RationalMatrix class, the add method is implemented by adding the two
numbers using the add method in the Rational class. The multiply method is
implemented by multiplying the two numbers using the multiply method in the
Rational class. The zero method is implemented to return new Rational(0, 1).
26. You have to define it using:

public static <T> void printResult(

T[][] m1, T[][] m2, T[][] m3, char op)

You might also like