Abhinav Kumar Singh
Abhinav Kumar Singh

Reputation: 182

Java Generics : Syntax Explanation

I am new to java generics my question is :

public static < E > void printArray( E[] inputArray )

In the above statement when the return type of function is void then why we have used E before void? Why this syntax is valid ?

As per theory a method could itself be an return type?

Then suppose E = method_abc then how compiler will manage

public static method_abc void printArray (E[] Array)

My reference program was

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

    public static void main( String args[] )
    {
        // Create arrays of Integer, Double and Character
        Integer[] intArray = { 1, 2, 3, 4, 5 };
        Double[] doubleArray = { 1.1, 2.2, 3.3, 4.4 };
        Character[] charArray = { 'H', 'E', 'L', 'L', 'O' };

        System.out.println( "Array integerArray contains:" );
        printArray( intArray  ); // pass an Integer array

        System.out.println( "\nArray doubleArray contains:" );
        printArray( doubleArray ); // pass a Double array

        System.out.println( "\nArray characterArray contains:" );
        printArray( charArray ); // pass a Character array
    } 
}

Upvotes: 2

Views: 1900

Answers (3)

rrevo
rrevo

Reputation: 1777

In public static < E > void printArray( E[] inputArray ), the E is just used to signify a Generic parameter. The return type of the method is still void.

Upvotes: 2

Dmitry Spikhalsky
Dmitry Spikhalsky

Reputation: 5820

By <E> you define a generic variable type, if you will write

public static void printArray( E[] inputArray )

Java will try to find a class with name E, because you don't define E as a generic. The return value in both cases are null, this <E> generic definition part in not a part of return value, it's a separate block of function signature.

Upvotes: 1

blalasaadri
blalasaadri

Reputation: 6188

The <E> here has nothing to do with the return type; it means that this is a generic function which can take various types of arrays. To be easier to understand, the code could be something like this:

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

    public static void main( String args[] )
    {
        // Create arrays of Integer, Double and Character
        Integer[] intArray = { 1, 2, 3, 4, 5 };
        Double[] doubleArray = { 1.1, 2.2, 3.3, 4.4 };
        Character[] charArray = { 'H', 'E', 'L', 'L', 'O' };

        System.out.println( "Array integerArray contains:" );
        GenericMethodTest.<Integer>printArray( intArray  ); // pass an Integer array

        System.out.println( "\nArray doubleArray contains:" );
        GenericMethodTest.<Double>printArray( doubleArray ); // pass a Double array

        System.out.println( "\nArray characterArray contains:" );
        GenericMethodTest.<Character>printArray( charArray ); // pass a Character array
    } 
}

So what the <E> does it tell the function "There is a generic type E and you accept arrays of E, so if for example I call you with the generic type Integer you accept Integer[]."

It does not change the return type though. Just like public static doesn't change the return type.

Upvotes: 2

Related Questions