Sub topic 13
Write Generic Methods
A generic method can work with more than one type while keeping the types of its inputs and result connected. This lets you reuse one method instead of writing a separate version for each type.
For example, a method that picks the first available value can use the same steps for a name or a score.
A method for different types
Save this program as GenericDemo.java:
class GenericDemo {
static <T> T firstNonNull(T first, T backup) {
if (first != null) {
return first;
}
return backup;
}
public static void main(String[] args) {
String name = firstNonNull(null, "Guest");
Integer score = firstNonNull(null, 7);
String drink = firstNonNull("Tea", "Water");
System.out.println(name);
System.out.println(score);
System.out.println(drink);
}
}Compile and run it from the folder containing the file:
javac GenericDemo.java
java GenericDemoThe output is:
Guest
7
TeaHere, null means there is no object. The method returns first if it is not null. Otherwise, it returns backup. If both are null, the method returns null.
Read the declaration
Look at the start of the method:
static <T> T firstNonNull(T first, T backup)
staticmeans you can call the method without creating an object of the class.<T>declares a type variable namedT. A type variable is a name that stands for a type.- The next
Tis the return type: it connects the result to the type chosen for the call. T firstandT backupare parameters, the named inputs inside the method.
The type variable is available within this method’s declaration and body. You can choose another name, but T is a common short name for “type.”
How Java chooses a type
An argument is a value passed to a method. In the name call, the arguments are null and "Guest". Java can use String as T, so the result can be stored in name.
In the score call, Java can use Integer as T. An Integer is an object that holds an int value. Java changes the argument 7 into an Integer automatically. This change is called boxing.
The compiler is the tool that checks your Java code and prepares it to run. It usually works out the type for T from the arguments and the expected result. This is called type inference.
Watch the result type
A generic method helps the compiler check how a result is used. For example, firstNonNull(null, "Guest") can return a String, so you can store it in a String variable without changing its type yourself.
Do not assume that both arguments must always have the exact same type. If they differ, Java may choose a broader type that can hold either one. In that case, the result may not fit into a more specific variable such as a String.
Also, T does not tell Java that the values support every operation. For example, you cannot assume that every possible T can be added with +. Use a generic method when the same steps make sense for several types, as they do in firstNonNull.
Exercise
What do firstNonNull("Ava", "Guest") and firstNonNull(null, null) return?
Show answer
The first call returns "Ava" because it is not null. The second returns null because its backup is also null.