Today I started to study generics, but for me this is something strange:
I have a general method:
public<T> HashMap<String, T> getAllEntitySameType(T type) {
System.out.println(type.getClass());
HashMap<String, T> result = null;
if(type instanceof Project)
{
System.out.println(type.toString());
System.out.println("Yes, instance of Project;");
}
if(type instanceof String)
{
System.out.println(type.toString());
System.out.println("Yes, instance of String;");
}
this.getProjects();
return result;
}
And I can easily define a class like T
Project<Double> project = new Project<Double>();
company2.getAllEntitySameType(project);
company2.getAllEntitySameType("TestString");
The output will be:
class Project
Yes, instance of Project;
class java.lang.String
TestString
Yes, instance of String;
I thought that in generics we cannot use an instance. Something is not fully known to me. Thank...
czupe source
share