20503

What does the type parameter in the method definition mean? [duplicate]

Question:

This question already has an answer here:

<ul><li> <a href="/questions/462297/how-to-use-classt-in-java" dir="ltr" rel="nofollow">How to use Class<T> in Java?</a> <span class="question-originals-answer-count"> 10 answers </span> </li> </ul> 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?<br /> 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.

Answer1:

It is a generic type parameter. See <a href="https://docs.oracle.com/javase/tutorial/java/generics/index.html" rel="nofollow">Oracle tutorial</a> about <em>Generics</em>.

Recommend

  • Regex for country code
  • Javascript regex. Need to extract value from a SVG string
  • Regex: get every number that doesn't has an other number before or after itself
  • Problem serializing DecimalFormatSymbols in 2.3.4
  • Issue with static code in Android
  • GUI not updating despite use of validate()
  • GridBagLayout padding
  • iOS Validation failed -> Bundle is invalid (Xcode 6.2, iOS 8.2)
  • logging static methods in a parent class
  • Problem with rejecting incoming call
  • If statement skipping right to else after being called once?
  • Reading a file into a multidimensional array
  • Read text file that is not in the main package in a runnable jar
  • Display java JPanel in a JFrame
  • App restarts from wrong activity
  • How to Cache Real-time Data?
  • Bad request using file_get_contents for PUT request in PHP
  • Exception “firebase.functions() takes … no argument …” when specifying a region for a Cloud Function
  • Jetty Server not starting: Unable to establish loopback connection
  • Why value captured by reference in lambda is broken? [duplicate]
  • Spring Data JPA custom method causing PropertyReferenceException
  • Possible to stop flickering java tooltip in heavyweight mode?
  • output of program is not same as passed argument
  • SSO with signing and signature validation doesn't work
  • Deserializing XML into class C#
  • sending/ receiving email in Java
  • How to set my toolbar fixed while scrolling android
  • Timeout for blocking function call, i.e., how to stop waiting for user input after X seconds?
  • Akka Routing: Reply's send to router ends up as dead letters
  • AT Commands to Send SMS not working in Windows 8.1
  • How to delete a row from a dynamic generate table using jquery?
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • Proper folder structure for lots of source files
  • Proper way to use connect-multiparty with express.js?
  • How does Linux kernel interrupt the application?
  • apache spark aggregate function using min value
  • unknown Exception android
  • Checking variable from a different class in C#
  • Sorting a 2D array using the second column C++
  • costura.fody for a dll that references another dll