Return Type of Java Generic Methods

user975343 picture user975343 · Jul 7, 2012 · Viewed 12.8k times · Source

I wonder why generic methods which return nothing void are (or can be) declared this way:

   public static <E> void printArray( E[] inputArray ) {
     // Display array elements              
     for ( E element : inputArray ){        
        System.out.printf( "%s ", element );
     }
     System.out.println();
   }

It seems like <E> is the type of the returned object, but the method returns nothing in fact. So what is the real meaning of <E> in this case specifically and in generic methods generally?

Answer

Jops picture Jops · Apr 4, 2013

This question suits one of my old notes. I hope this illustration helps:

enter image description here enter image description here