2

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
    } 
}

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

[Blalasaadri] Sir Thank you :)
2

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.

1 Comment

[Rrevo] Sir Than you :)
1

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.

1 Comment

[Dmitry Spikhalskiy] Thank you sir :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.