Non-Generic Method Example - maxNonGeneric()

This section provides a tutorial example of non-generic method, maxNonGeneric(), that shows type compatibility mistakes are not detected at the compilation time.

To support our analysis provided in the previous section, I wrote the following tutorial example to implement and test the non-generic method:

/* MaxNonGenericMethod.java
 - Copyright (c) 2014, HerongYang.com, All Rights Reserved.
 */
class MaxNonGenericMethod {
   public static Comparable maxNonGeneric(
      Comparable a, Comparable b) {
      if (a.compareTo(b)>=0) {
         return a;
      } else {
      	 return b;
      }
   }
   public static void main(String[] a) {
      String max = (String) maxNonGeneric(
         new String("1234"), new String("789"));
      System.out.println("Maximum: "+max);

      // Mistake 1: Runtime exception when calling compareTo()
      String max1 = (String) maxNonGeneric(
         new String("1234"), new Integer("789"));

      // Mistake 2: Runtime exception when casting return value
      Integer max2 = (Integer) maxNonGeneric(
         new String("1234"), new String("789")); 
   }
}

If you compile and run this tutorial example, you will get:

C:\herong>javac MaxNonGenericMethod.java

Note: MaxNonGenericMethod.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

C:\herong>java MaxNonGenericMethod

Maximum: 789
Exception in thread "main" java.lang.ClassCastException: 
   java.lang.Integer cannot be cast to java.lang.String
        at java.lang.String.compareTo(String.java:111)
        at MaxNonGenericMethod.maxNonGeneric(MaxNonGenericMethod.java:7)
        at MaxNonGenericMethod.main(MaxNonGenericMethod.java:19)

As you can see from the output, the compiler did not catch the first type compatible mistake in the (String max1 = (String) maxNonGeneric(new String("1234"), new Integer("789"));) call. The resulting bytecode ran into a class cast exception when compareTo() was called.

If comment out line 20 and run the program again:

C:\herong>javac MaxNonGenericMethod.java

Note: MaxNonGenericMethod.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

C:\herong>java MaxNonGenericMethod

Maximum: 789
Exception in thread "main" java.lang.ClassCastException: 
   java.lang.String cannot be cast to java.lang.Integer
        at MaxNonGenericMethod.main(MaxNonGenericMethod.java:23)

As you can see from the output, the compiler did not catch the second type compatible mistake in the (Integer max2 = (Integer) maxNonGeneric(new String("1234"), new String("789"));) call. The resulting bytecode ran into another class cast exception when the return value was assigned to "max2".

Conclusion: Non-generic methods can leave type compatibility mistakes undetected at compilation time. This leads to high likelihood of runtime exceptions.

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