Comparing Generic Method with Non-Generic Method

This section provides a tutorial example of comparing a generic method and a non-generic method for the same requirement. The generic method allows the compiler to catch more type compatibility mistakes.

To see advantages of using generic methods, let's try to develop a non-generic method and a generic method for the same requirement and compare them.

Assuming that we have a requirement to develop a static method that compares two objects and returns the object that has the higher value, and this method should support multiple types of objects like String, Number, Date, Time, etc., here is the best I can write as a non-generic version of the method:

public static Comparable maxNonGeneric(Comparable a, Comparable b) {}

This definition is very weak in terms of type requirements in the method signature. When programmers try to invoke this non-generic method, they have chances of making mistakes as listed below. The compiler will not be to catch those mistakes. The resulting bytecode will be more likely running into runtime exceptions:

String max = (String) maxNonGeneric(
   new String("1234"), new String("789")); // Correct invokation

String max = (String) maxNonGeneric(
   new String("1234"), new Integer("789")); // Run-time exception

Integer max = (Integer) maxNonGeneric(
   new String("1234"), new String("789")); // Run-time exception

Here is my generic version of the method for the same requirement:

public static <T extends Comparable<T>> T maxGeneric(T a, T b) {}

This definition is much stronger now in terms of type requirements in the method signature:

Now when programmers try to invoke this generic method, they still have chances of making those same mistakes. But the compiler will catch them because of the stronger definition of the method. The resulting bytecode will be less likely running into runtime exceptions

String max = SomeClass.<String>maxGeneric(
   new String("1234"), new String("789")); 
   // Correct invokation. No (String) casting needed in source code.

Integer max = SomeClass.<String>maxGeneric(
   new String("1234"), new String("789")); // Compilation error
   
String max = SomeClass.<String>maxGeneric(
   new String("1234"), new Integer("789")); // Compilation error

Note that the class name "SomeClass" is needed because the type argument <String> is required to show up after the dot (.) operator.

Last update: 2014.

Table of Contents

 About This Book

 Installing JDK 1.8 on Windows

 Execution Process, Entry Point, Input and Output

 Primitive Data Types and Literals

 Bits, Bytes, Bitwise and Shift Operations

 Managing Bit Strings in Byte Arrays

 Reference Data Types and Variables

 StringBuffer - The String Buffer Class

 System Properties and Runtime Object Methods

 Generic Classes and Parameterized Types

Generic Methods and Type Inference

 What Is a Generic Method?

Comparing Generic Method with Non-Generic Method

 Non-Generic Method Example - maxNonGeneric()

 Generic Method Example - maxGeneric()

 Generic Methods in java.util.Collections Class

 Testing Generic Methods in Collections Class

 What Is Type Argument Inference?

 Type Argument Inference by Parameter List

 Type Argument Inference by Return Value

 Generic Methods using Parameterized Types

 Parameterized Type as Generic Method Return Type

 Lambda Expressions and Method References

 Execution Threads and Multi-Threading Java Programs

 ThreadGroup Class and "system" ThreadGroup Tree

 Synchronization Technique and Synchronized Code Blocks

 Deadlock Condition Example Programs

 Garbage Collection and the gc() Method

 Outdated Tutorials

 References

 PDF Printing Version