Easy learning with example program codes

Generics method example with two parameters

The methods which can handle different types of arguments are known as generic methods. Types of the argument are specified on method call.

Syntax: generic method can includes comma separated multiple type parameter, inside angle brackets and appears before the method’s return type.

public static <T, U> T identical(T obj1, U obj2){
        return returnValue;


 * This class is used to show the use of 
 * generics method with two parameters.
 * @author codesjava
class Test {
  //Generics method with two parameters.	
  public <T, U> void showItemDetails(T itemT, U itemU){
	  System.out.println("Value of the itemT: " + itemT);
	  System.out.println("Type of the itemT: " 
			  + itemT.getClass().getName());
	  System.out.println("Value of the itemU: " + itemU);
	  System.out.println("Type of the itemU: " 
			  + itemU.getClass().getName());
public class GenericsTest {	
	public static void main(String args[]){
		//String type test
		Test test = new Test();
		test.showItemDetails("Test String.", 100);


Value of the itemT: Test String.
Type of the itemT: java.lang.String
Value of the itemU: 100
Type of the itemU: java.lang.Integer

Download this example.
Next Topic: Generics constructor example.
Previous Topic: Generics method example.

Related Topics:

How to build java project using ant in eclipse?
JAXB marshalling – convert java object to xml example.
How to create pdf file in java using iText jar?
Generics class example.
OGNL in struts 2.
Hibernate One-to-One Mapping using xml.
Send inline image in email using JavaMail API.
Quartz 2 JobListener example.


Copyright © 2018 CodesJava Protection Status SiteMap