Created by Nathan Hunsdale
almost 10 years ago
|
||
Generics are a facility of generic programming that was added to Java as part of _____
They allow a type or method to operate on objects of various _____
Generics also provides _____-_____ type saftey
Generics
The Motivation
No matter how careful one is at programming, _____ will always exist
Generics
The Motivation
______-______ bugs tell you immediately that something wrong
Generics
The Motivation
______ bugs are much more problematic as they don't always surface _____. When they do surface, it might be at a time that is far removed from where the bug originated
Generics
The Motivation
Generics add _____ to your code by making more of your bugs detectable at _____-_____
Generics
The Motivation
Error Catching
The main motivation is to generate _____-_____ errors rather than _____ errors
Generics
The Motivation
Replacement for Method Overloading
Overloaded methods are often used to perform similar operations on different types of _____
Generics
The Motivation
What is an overloaded method?
Generics
The Motivation
e.g. 2 overloaded methods could be replaced with one method that accepts a non-specific _____
Generics
The Motivation
An example of how to replace 2 overloaded methods with one generic method
Generics
Implementation and Compile-Time Translation
Based on the types of the arguments passed to the _____ _____,the compiler handles each method call appropriately
Generics
Implementation and Compile-Time Translation
Create a generic method called printMe()
Generics
Implementation and Compile-Time Translation
The <T> marks the method as being _____ and specifies the name of the <T> parameter that will be used in the definition
Generics
The Motivation
It will only serve as a replacement for overloaded methods that perform the exact same _____
Generics
When the compiler translates a generic method into Java bytecodes, it removes the type parameter and replaces with actual types. This process is known as _____
Type parameters used in a method's parameter list
The type-parameter names throughout the method declaration must match those declared in the _____-_____ section
Generics
Declaring more than 1 type parameter
Create a method that takes two type parameters:
Generics
Methods that use a parameter as the Return Type
Can a relational operator be used with return types?
Generics
Methods that use a parameter as the Return Type
It is possible to compare 2 objects of the same class if that class implements the generic interface _____
Generics
Methods that use a parameter as the Return Type
Example of Comparable
Generic Classes
Creating a String variable at the top of the class (e.g. Private String Name;) means that it can only take a _____ as its content
Generic Classes
public class GenericBox <T>{
Generically defining a class (as above) enables a lot more flexibility and also includes _____-_____ saftey
Generic Classes
Compile-Time Safety
At compile time, the compiler ensures the _____ safety of your code and uses erasure techniques to enable a more usable system.