1
public class GenericMethods {
    public <T> void f(T x) {
        System.out.println(x.getClass().getName());
    }
    public static void main(String[] args) {
        GenericMethods gm = new GenericMethods();
        gm.f("");
        gm.f(1);
        gm.f(1.0);
        gm.f(1.0F);
        gm.f('c');
        gm.f(gm);
    }
}
/* Output:  
java.lang.String  
java.lang.Integer  
java.lang.Double  
java.lang.Float  
java.lang.Character  
GenericMethods  */

What does public <T> void f(T x) mean? Is it a return type?
But the function doesn't actually return anything. What does it refer to? I couldn't place it under any of the general function header categories : like acces-specifier, return-type,etc.

Prabodh Mhalgi
  • 805
  • 9
  • 25
Ram
  • 23
  • 1
  • 5
  • 5
    It's a generic type parameter, declaring that the method is generic, that's all. See https://docs.oracle.com/javase/tutorial/java/generics/methods.html – Jon Skeet Oct 27 '16 at 08:33

1 Answers1

1

It is a generic type parameter. See Oracle tutorial about Generics.

Anatoly Shamov
  • 2,608
  • 1
  • 17
  • 27
Prabodh Mhalgi
  • 805
  • 9
  • 25