Generics Flashcards
1
Q
Explain what generics are and why raw types should be avoided.
A
- Generics is introduced with java 1.5. Any class and instance that allow one or more parameterized type is a generic.
- Generics provide compile-time type safety and should always be preferred over raw types.
- A parameterized type does not exhibit inheritance. You cannot pass List to method expecting List since the method can add objects into the list. Similarly, you cannot pass List into method expecting List since the method may try to take out a String.
- If you don’t care what type the parameterized type is use ?. This allow you to take objects out but not add anything to the generics.
- If you need to add something to generics you must use bounded wild card type such as ? extends ClassName.
- Raw type should be modified to be parameterized wherever you find time for type safety.
2
Q
Explain why unchecked warnings should be avoided and how to do so.
A
- Unchecked warning is a clue that a type may be used in an unsafe manner, leading to classcastexception when it’s being casted out.
- Therefore when you see unchecked warning, you should fix it by adding generics wherever you find it.
- If this cannot be fixed, make sure to add suppresswarning annotation to make the warning message go away so that it does not clutter up the logs.
3
Q
Explain why list and arrays do not mix and why list should be preferred.
A
- Arrays exhibit inheritance. So Object[] = new Long[1] is legal. Type is enforced at runtime, so adding something not Long to Object[] would compile, but fail at runtime instead.
- On the other hand, generics enforce their behavior at compile time and erase their type at runtime.
- If you declare List[] and assign it to Object[], that would be legal because Arrays exhibit inheritance. Now if you add List to that array, that would be fine since arrays enforce their type at runtime. Now we have a problem! The generics declaration wasn’t obeyed. Because of this, java doesn’t allow declaring an array or generics.
- Whenever you are trying to mix array and generics, try to convert everything to list instead. This gives you better type safety and no unchecked warnings.
- one example is method with varargs argument. The varargs is converted to array, so you can’t have varargs with generics.
4
Q
Explain why generic types and methods should be favored and how to use them
A
- Generic type and methods make a class’s api more flexible. It’s pretty easy to turn a normal class into a generic class by introducing parameterized types whenever appropriate. Prime candidate for these conversions are container classes that contain a single type of object.
- Generic methods are also very useful, especially with static methods. Java do type inference automatically with generic methods when you assign something to it as well as when you assign the result of the generic method to a generic variable. This makes static factory methods very easy to read.
5
Q
Explain how to use bounded wildcard to increase api flexibility
A
- Generic of one type isn’t a super or subtype of another type. For example, Iterable is not a subtype of Iterable. This can sometimes lead to inflexible API.
- As a general rule of thumb, an API can be increased in flexibility by changing these generic parameters to use bounded wildcard type where appropriate.
- You can determine when it’s appropriate to use wildcard type on generics by following the PECS rule: Producers extends. Consumer super. If the method in question will be taking value out of the variable, the variable is a producer and the declaration can use extends. On the other hand, if the method is going to put value into the variable, the variable is a consumer and can use super. If the variable does both, then it must just use the generic type without extend or super.