Generics Flashcards

1
Q

How can we restrict what class or interface could be used as a type argument in generics?

A

We can accomplish this by assigning an upper bound. An upper bound will limit the type parameter to a parent type or any of its child types. Let’s see how this is done:

public class Box <T extends Number> {
  private T data; 
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How can we use upper bounds with generic methods?

A
public static <T extends Number> boolean isZero(T data) {
  return data.equals(0);
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How do we create a type parameter with multiple bounds with generics?

A
public class Box <T extends Number & Comparable<T>> {
  private T data; 
}

Here we specify multiple bounds (Number and Comparable) for type parameter T using the & operator between the different upper bounds. It’s important to note that when defining multiple bounds, any upper bound that is a class, in our example Number, must come first, followed by any interfaces, in our example Comparable<T>.</T>

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

What is wildcards used for in generics?

A

We can make our code even more general when we don’t need the more strict type checking of using type parameters by using wildcards. A wildcard, denoted by the ? symbol, represents an unknown type when used with generic methods.

public class Util {
  public static void printBag(Bag<?> bag ) {
    System.out.println(bag.toString()); 
  }
}
Bag<String> myBag1 = new Bag("Hello");
Bag<Integer> myBag2 = new Bag(23);
Util.printBag(myBag1);  // Hello
Util.printBag(myBag2);  // 23
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is a lower-bound wildcard?

A

We can also provide a lower bound when working with wildcards. A lower bound wildcard restricts the wildcard to a class or interface and any of its parent types. For example:

public class Util {
  public static void getBag(Bag<? super Integer> bag ) {
    return bag;
  }
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are some important things to note about lower bounds?

A
  • They cannot be used with generic type parameters, only wildcards.
  • A wildcard cannot have both a lower bound and upper bound, in this case, it’s best to use a type parameter.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

When and how should we use wildcards?

A
  • An upper-bound wildcard should be used when the variable is being used to serve some type of data to our code.
  • A lower bound wildcard should be used when the variable is receiving data and holding it to be used later.
  • When a variable that serves data is used and only uses Object methods, an unbounded wildcard is preferred.
  • When a variable needs to serve data and store data for use later on, a wildcard should not be used (use a type parameter instead).
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How do we define generics?

A

The diamond operator (<>) is used to define generic classes, interfaces, or methods.

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

What is the advantages of using generics?

A
  • Generics provide compile-time type safety and bug detection as opposed to raw types.
  • Generics remove the need to explicitly type cast when working with generics types.
  • Generics allow you to create algorithms that don’t depend on a specific data type through methods, interfaces, and classes.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Can we use generics with primitive types?

A

No. Only wrapper classes are provided to allow primitive values to be used with generic code.

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