Generic Method

  • Eclipse: Oxygen
  • Java: 1.8

This tutorial covers the generics methods. The methods are used here can serve their functionality to all the types of Objects which are shown here.

Java’s generic methods allow programmers to specify, with a single method declaration, a set of related methods or, with a single class declaration, a set of related types.

The following program demonstrates that we have written a single generic method statement that can be called with arguments of different types.

All declarations of generic methods have a section of parameters of type delimited by angular brackets (<E>) that precedes the method’s return type.

It can be used to declare the type of return. The type parameter can represent only reference types, not primitive types (such as int, double, and char).

Output

Printing integer list

******************

1

2

3

4

Printing String list

******************

One

Two

Three

Four

Printing String array

******************

One

two

Three

Printing integer array

******************

1

2

3

4

Printing double array

******************

2.0

5.0

4.6

8.5

Note: Each type parameter section contains one or more type parameters (<E, T>) separated by commas.

Contributed by: Poonam Tomar


Full stack Java Developer

   Buy me a coffee!

 

PayTM QR (INDIA)

Download file

Leave a Reply

avatar
  Subscribe  
Notify of
Close Menu