Skip to content

Instantly share code, notes, and snippets.

@koher
Last active February 21, 2017 06:27
Show Gist options
  • Save koher/4484ac72995a92a29a056d9f6401eec8 to your computer and use it in GitHub Desktop.
Save koher/4484ac72995a92a29a056d9f6401eec8 to your computer and use it in GitHub Desktop.
public class GenericReturnType {
public static void main(String[] args) {
// 引数の型は `Object` なので戻り値から型パラメータが推論される
Integer n = id(42);
String s = id("xyz");
System.out.println(n);
System.out.println(s);
}
static <T> T id(Object x) {
return (T)x;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment