JAVA
java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/*
Subject:
- Create a method that takes a generic datatype (a type that doesn't need be specified at compile time)
- Pass different types of arguments to that method
- notice that the method is not overloaded, you can pass any type to it!
- Output type and value of the argument
*/
public class GenericsExample{
public static void main(String[] args) {
int i=1;
checkType(i);
float f=2f;
checkType(f);
double d=3.0;
checkType(d);
String s="Hello world!";
checkType(s);
}
public static <T> void checkType(T arg) {
System.out.println("Type is " +
arg.getClass().getSimpleName() +
Enter to Rename, Shift+Enter to Preview
OUTPUT
Run