Register Now

Login

Lost Password

Enter your email to reset your password.

BY Author

Java – Generics with Multiple type Parameters

It is possible to use multiple parameters with different types in a generic code.

Example

 public class MultiParaTypes<T,V,S> 
 {
     T var1;
     V var2;
     S var3;
	
     public void input(T var1, V var2, S var3)
     {
         this.var1 = var1;
         this.var2 = var2;
         this.var3 = var3;
     }
	
     public T output1()
     {
         return var1;
     }
	
     public V output2()
     {
         return var2;
     }

     public S output3()
     {
         return var3;
     }

     public static void main(String args[])
     {
         MultiParaTypes<String,Integer,Double> gt1 = new
                                             MultiParaTypes<String,Integer,Double>();
		
         gt1.input("Jack", 22, 5.11);
		
         String s = gt1.output1();
         int i = gt1.output2();
         double d = gt1.output3();
		
         System.out.println(s+"'s age is "+i+" & height is "+d);
     }
 }

Output

Jack's age is 22 & height is 5.11

Now in the above program look at the starting line,

  public class MultiParaTypes<T,V,S>

It contains three generic type parameters (TV & S) separated by a comma. It means in a generic type we can have more than one type parameters.

Therefore we also need to make a change in the line that is used to pass type arguments to MultiParaTypes’s type. The line is,

MultiParaTypes<String,Integer,Double> gt1 = new MultiParaTypes<String,Integer,Double>();

Here through this line we have created an object of type MultiParaTypes and passed type arguments String, Integer & Double to the type parameters <T,V,S> declared at first line.

Let’s see how it works, Java Programming Language Generics with Multiple Parameters

Java compiler removes all the T and replaces by String. Therefore program behaves as if it already contains String type wherever T is located.

Java compiler removes all the V and replaces by Integer. Therefore program behaves as if it already contains Integer type wherever V is located.

Java compiler removes all the S and replaces by Double. Therefore program behaves as if it already contains Double type wherever S is located, as shown below:

 

 public class MultiParaTypes 
 {
     String var1;
     Integer var2;
     Double var3;
	
     public void input(String var1, Integer var2, Double var3)
     {
         this.var1 = var1;
         this.var2 = var2;
         this.var3 = var3;
     }
	
     public String output1()
     {
         return var1;
     }
	
     public Integer output2()
     {
         return var2;
     }

     public Double output3()
     {
         return var3;
     }

     public static void main(String args[])
     {
         MultiParaTypes gt1 = new MultiParaTypes();
		
         gt1.input("Jack", 22, 5.11);
		
         String s = gt1.output1();
         int i = gt1.output2();
         double d = gt1.output3();
		
         System.out.println(s+"'s age is "+i+" & height is "+d);
     }
 }

 

Leave a reply