Java Generics Flashcards

(6 cards)

1
Q

What is a “generic” in Java?

A

A generic class or method is one that permits you to specify the allowable types of objects that the class or method may work with. If you attempt to use the class or method with an object that is not of an allowable type, an error occurs at compile time.

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

What are the benefits of using Java Generics?

A
  1. Code Reuse: We can write a method/class/interface once and use for any type we want.
  2. Type Safety : Generics make errors appear at compile time as opposed to at run time (It’s always better to know problems in your code at compile time rather than making your code fail at run time). Suppose you want to create an ArrayList that store name of students and if by mistake programmer adds an integer object instead of string, then the compiler allows it. But, when we retrieve this data from ArrayList, it causes problems at runtime.
  3. Individual Type Casting isn’t Needed: If we do not use generics, then, in the above example every-time we retrieve data from ArrayList, we have to typecast it. Typecasting at every retrieval operation is a big headache. If we already know that our list only holds string data then we need not to typecast it every time.
  4. Implementing Generic Algorithms: By using generics, we can implement algorithms that work on different types of objects and at the same they are type safe too.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is a type parameter?

A

A type parameter is an identifier in a generic class or method that represents and can be replaced with an actual type. It is considered good style to use upper case letters for type parameters and abide by these common conventions:

  • E - Element type in a collection
  • K - Key type in a map
  • V - Value type in a map
  • T - General type
  • S & U - Additional general types
Note that type variables follow the class name and are enclosed in angle brackets, such as:
*public class MyArrayList<e> </e>*
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How can you constrain type parameters?

A

Type parameters can be constrained with bounds by implementing an interface. For example, if you want to ensure that an object calling a calcAverage() method has a mechanism for measuring the elements, then you can implement a Measurable interface with a getMeasure() method to return the applicable data for any object that implements it.
For example, with a bank account it could be the balance and for a sports team could be the number of players.

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

What is “type erasure”?

A

The Java virtual machine does not work with generic classes or methods. Instead, type parameters are “erased” or replaced with ordinary Java types. Each type parameter is replaced with its bound or with an Object if it is not bounded.

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

Can you create objects of a generic type?

A

Yes.

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